什么是变量

A variable is a name for a place in the computer's memory where you store some data.

变量是计算机内存中存储某些数据的位置的名称。

Imagine a very large warehouse with lots of storage bays, tables, shelves, special rooms etc. These are all places where you can store something. Let's imagine we have a crate of beer in the warehouse. Where exactly is it located?

想象一下一个非常大的仓库,里面有很多储藏室,桌子,架子,特殊房间等。这些都是可以存放东西的地方。 假设我们仓库里有一箱啤酒。 它到底在哪里?

We wouldn't say that it is stored 31' 2" from the west wall and 27' 8" from the north wall. In programming terms we also wouldn't say that my total salary paid this year is stored in four bytes starting at location 123,476,542,732 in RAM.

我们不会说它存储在距西墙31'2“和距北墙27'8”的位置。 在编程方面,我们也不会说我今年的总薪水以四个字节存储在RAM中的123,476,542,732处。

PC中的数据 ( Data in a PC )

The computer will place variables in different locations each time our program is run. However, our program knows exactly where the data is located. We do this by creating a variable to refer to it and then let the compiler handle all the messy details about where it is actually located. It is far more important to us to know what type of data we will be storing in the location.

每次运行程序时,计算机会将变量放置在不同的位置。 但是,我们的程序确切知道数据的位置。 为此,我们创建一个变量来引用它,然后让编译器处理有关它实际位置的所有杂乱细节。 对于我们而言,知道我们将在该位置存储什么类型的数据更为重要。

In our warehouse, our crate might be in section 5 of shelf 3 in the drinks area. In the PC, the program will know exactly where its variables are located.

在我们的仓库中,我们的板条箱可能位于饮料区域中3号货架的第5部分。 在PC中,程序将确切知道其变量的位置。

变量是临时的 ( Variables Are Temporary )

They exist just as long as they are needed and are then disposed of. Another analogy is that variables are like numbers in a calculator. As soon as you hit the clear or power off buttons, the display numbers are lost.

只要它们需要它们就存在,然后将其处置。 另一个比喻是变量就像计算器中的数字。 按下清除或关闭电源按钮后,显示编号就会丢失。

变量有多大 ( How Big Is a Variable )

As big as is needed and no more. The smallest a variable can be is one bit and the largest is millions of bytes. Current processors handle data in chunks of 4 or 8 bytes at a time (32 and 64 bit CPUs), so the bigger the variable, the longer it will take to read or write it. The size of the variable depends on its type.

足够大,仅此而已。 变量最小可以为一位,最大变量为数百万个字节。 当前的处理器一次处理4或8个字节的数据块(32和64位CPU),因此变量越大,读取或写入数据所花费的时间就越长。 变量的大小取决于其类型。

什么是变量类型? ( What Is a Variable Type? )

In modern programming languages, variables are declared to be of a type.

在现代编程语言中 ,变量被声明为类型。

Apart from numbers, the CPU does not make any kind of distinction between the data in its memory. It treats it as a collection of bytes. Modern CPUs (apart from those in mobile phones) can usually handle both integer and floating point arithmetic in hardware. The compiler has to generate different machine code instructions for each type, so knowing what the type of variable helps it generate optimal code.

除了数字外,CPU在其内存中的数据之间没有任何区别。 它将其视为字节的集合。 现代的CPU(除了手机中的CPU)通常可以在硬件中处理整数和浮点运算。 编译器必须为每种类型生成不同的机器代码指令,因此了解变量的类型有助于其生成最佳代码。

变量可以保存哪些类型的数据? ( What Types of Data Can a Variable Hold? )

The fundamental types are these four.

基本类型是这四种。

  • Integers (both signed and unsigned) 1,2,4 or 8 bytes in size. Usually referred to as ints.

    整数 (有符号和无符号)1,2,4或8个字节。 通常称为整数。

  • Floating Point Numbers up to 8 bytes in size.

    浮点数,最大8个字节。

  • Bytes. These are organized in 4s or 8s (32 or 64 bits) and read in and out of the CPU's registers.

    字节数 。 它们以4s或8s(32或64位)进行组织,并读入和读出CPU的寄存器。

  • Text strings, up to billions of bytes in size. CPUs have special instructions for searching through large blocks of bytes in memory. This is very handy for text operations.

    文本字符串,最大数十亿字节。 CPU具有用于搜索内存中大块字节的特殊指令。 这对于文本操作非常方便。

There is also a general variable type, often used in scripting languages.

还有一种通用变量类型,通常在脚本语言中使用。

  • Variant - This can hold any type but is slower to use.

    变体 -可以容纳任何类型,但使用起来较慢。

数据类型示例 ( Example of Data Types )

  • Arrays of types- single dimension like drawers in a cabinet, two-dimensional like post office sorting boxes or three dimensional like a pile of beer crates. There can be any number of dimensions, up to the limits of the compiler.类型数组-单一尺寸(例如,柜子中的抽屉),二维(例如,邮局分拣箱)或三维(例如,一堆啤酒箱)。 可以有任意数量的尺寸,最高可达编译器的限制。
  • Enums which are a restricted subset of integers. Read about what is an enum is.

    枚举是整数的受限子集。 了解什么是枚举 。

  • Structs are a composite variable where several variables are lumped together in one big variable.结构是一个复合变量,其中几个变量集中在一个大变量中。
  • Streams provide a way to manage files. They're a form of a string.

    流提供了一种管理文件的方法。 它们是字符串的形式。

  • Objects, are like structs but with much more sophisticated data handling.

    对象就像结构,但具有更复杂的数据处理。

变量存储在哪里? ( Where are Variables Stored? )

In memory but in different ways, depending on how they are used.

在内存中的使用方式不同,取决于使用方式。

  • Globally. All parts of the program can access and change the value. This is how older languages like Basic and Fortran used to handle data and it is not considered a good thing. Modern languages tend to discourage global storage though it is still possible.在全球范围内。 程序的所有部分都可以访问和更改值。 这就是像Basic和Fortran这样的较旧的语言用来处理数据的方式,这并不是一件好事。 尽管仍有可能,现代语言往往会阻止全球存储。
  • On the Heap. This is the name for the main area used. In C and C++, access to this is via pointer variables.

    在堆上。 这是所用主要区域的名称。 在C和C ++中,可通过指针变量进行访问。

  • On the Stack. The stack is a block of memory that is used to store parameters passed into functions, and variables that exist local to functions.

    在堆栈上 。 堆栈是一块内存,用于存储传递给函数的参数以及存在于函数本地的变量。

结论 ( Conclusion )

Variables are essential to procedural programming, but it is important not to get too hung up on the underlying implementation unless you are doing systems programming or writing applications that have to run in a small amount of RAM.

变量对于过程编程是必不可少的,但是重要的是不要过于依赖底层实现,除非您正在进行系统编程或编写必须在少量RAM中运行的应用程序。

Our rules regarding variables:

我们关于变量的规则:

  1. Unless you are tight on ram or have large arrays, stick with ints rather than a byte (8 bits) or short int (16 bits). Especially on 32 Bit CPUs, there is an extra delay penalty in accessing less than 32 bits.

    除非您精通ram或有大数组 ,否则请使用int而不是字节 (8位)或short int (16位)。 特别是在32位CPU上,访问少于32位会有额外的延迟损失。

  2. Use floats instead of doubles unless you need the precision.

    除非需要精度,否则请使用浮点数而不是双精度数。

  3. Avoid variants unless really necessary. They are slower.除非确实需要,否则请避免使用变体。 他们比较慢。

翻译自: https://www.thoughtco.com/what-is-a-variable-958334

什么是变量

什么是变量_什么是变量?相关推荐

  1. 关于python变量_关于python变量练习题

    第一题(数字相加) age=20 new_age=age+1 print(new_age) 第二题(字符串相加) name='xiu' new_name=name+'hb' print(new_nam ...

  2. java线程条件变量_使用条件变量(多线程笔记)

    条件变量属性: 使用条件变量可以以原子方式阻塞线程,知道某个特定条件为真为止.条件变量始终与互斥锁一起使用. 使用条件变量,线程可以以原子方式阻塞,知道满足某个条件为止.对掉件的测试时在互斥锁的保护下 ...

  3. 力控批量添加变量_力控变量.ppt

    力控变量 3.1 变量类别 1.窗口中间变量 2.中间变量 3.数据库变量 4.间接变量 5.系统变量 第1页/共18页 3.1.1 窗口中间变量 窗口中间变量作用域仅限于力控应用程序的一个窗口,或者 ...

  4. python3环境变量_配置环境变量切换到python3.7

    安装过程homebrew install python3 本文主要介绍2.7切换到3.7. 如果没安装3.7,终端显示为: technology-fly:~ lcm$ python3 -bash: p ...

  5. python3.7.3配置环境变量_配置环境变量切换到python3.7

    安装过程homebrew install python3 本文主要介绍2.7切换到3.7. 如果没安装3.7,终端显示为: technology-fly:~ lcm$ python3 -bash: p ...

  6. python结构体变量_结构体变量 和 结构体指针(示例代码)

    为什么结构体在声明变量不需要去动态的分配内存,而声明结构体指针后,如果对成员进行操作需要分配内存,或者需要定义. 首先变量的声明与定义: extern int i; //声明,不是定义 int i; ...

  7. 01.Python基础_菜单_快捷键_基本语法_变量_输入输出

    文章目录 1.编程软件-jupyter notebook 2.基本说明 2.1 标题 2.2 排序 2.3Jupyter notebook使用/快捷键 2.4Markdown的基本语法 3.pytho ...

  8. MySQL数据库变量_数据库参数_MySQL变量_系统变量_用户变量

    文章目录 MySQL 变量分类 系统变量 查看系统变量 设置系统变量 如何通过配置文件来设置变量值 通过命令行选项来设置变量值 动态设置全局级的系统变量 设置静态的系统变量 设置会话级的系统变量 引用 ...

  9. python 定义变量_第三章(第2节):变量和常量

    变量的概念基本上和初中代数的方程变量是一致的,只是在计算机程序中,变量不仅可以是数字,还可以是任意数据类型,比如我们上节课刚刚学过的基本数据类型或者我们后面要学的自定义数据类型. 所谓常量就是不能改变 ...

最新文章

  1. BREW中的安全性网络编程
  2. jQuery操作CSS
  3. 在csdn上关于Silverlight有用文章收集
  4. 天津盈克斯机器人科技_网红新科技,走进家居新时代|环渤海爱乐屋门窗amp;威卢克斯天窗双旦狂欢节送您一个温暖的家!...
  5. 前端教程丨手把手教你用 Next.js 搭建个人博客,从入门到吃鸡
  6. Linux抓包工具tcpdump命令详解
  7. 研究综述 | 知识图谱划分算法研究综述
  8. python3 面向对象_Python3 面向对象
  9. layui弹出层:倒计时后自动关闭(含代码、案例)
  10. 【Python】Python第三方库安装
  11. 【unity】Inspector视图中的get/set使用(四)
  12. 计算机编程方面的电子书大汇总 阿里云盘
  13. 超星尔雅不让下载?课件,拿来吧你!
  14. 高效记忆/形象记忆(06)英语单词记忆-字母编码
  15. 英语海报简笔php匹配img画,简单英语海报图片手绘,一年级英语海报图片 手绘?...
  16. 《木偶奇遇记》读后感
  17. SparkSQL内置函数
  18. Python爬虫学习(四)爬取图片素材网站素材
  19. IT 战略规划-方法论
  20. 别说眼科医生不做近视激光手术,眼科医生做给你看

热门文章

  1. 学计算机对数学有要求,这4个专业,对数学要求很高,数学不好的慎选!
  2. 不预览直接打印 Microsoft RDLC报表
  3. 有向图的必经点,支配树
  4. blockly android教程,【blockly入门指引】3,android-入门
  5. 属性操作与元素节点操作和onscroll
  6. 【git】git拉取远程分支
  7. SaaSBase:推荐一些超好用的SCRM社交客户管理软件(中篇)
  8. 文王金钱六十四卦以及Java算卦程序
  9. 5G中使用的计算虚拟化技术的介绍
  10. Ubuntu 21.10 ALC294 声卡无声音、音量只能最大或静音问题的解决办法