java中变量名声明变量

A variable is a container that holds values that are used in a Java program. To be able to use a variable it needs to be declared. Declaring variables is normally the first thing that happens in any program.

变量是一个容器,其中包含Java程序中使用的值。 为了能够使用变量,需要对其进行声明。 声明变量通常是任何程序中发生的第一件事。

( How to Declare a Variable )

Java is a strongly typed programming language. This means that every variable must have a data type associated with it. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or boolean.

Java是一种强类型的编程语言。 这意味着每个变量都必须具有与其关联的数据类型。 例如,可以声明一个变量以使用以下八种原始数据类型之一 :字节,短整数,整数,长整数,浮点数,双精度,字符或布尔值。

A good analogy for a variable is to think of a bucket. We can fill it to a certain level, we can replace what's inside it, and sometimes we can add or take something away from it. When we declare a variable to use a data type it's like putting a label on the bucket that says what it can be filled with. Let's say the label for the bucket is "Sand". Once the label is attached, we can only ever add or remove sand from the bucket. Anytime we try and put anything else into it, we will get stopped by the bucket police. In Java, you can think of the compiler as the bucket police. It ensures that programmers declare and use variables properly.

一个变量的一个很好的类比是一个存储桶。 我们可以将其填充到一定级别,可以替换其中的内容,有时可以添加或删除其中的某些内容。 当我们声明一个变量以使用数据类型时,就像在标签上放置了一个标签,上面写着可以填充的内容。 假设桶的标签是“ Sand”。 贴上标签后,我们只能从桶中添加或去除沙子。 每当我们尝试放入其他物品时,我们都会被水桶警察拦住。 在Java中,您可以将编译器视为存储桶策略。 它确保程序员正确声明和使用变量。

To declare a variable in Java, all that is needed is the data type followed by the variable name:

要在Java中声明一个变量,所需要做的就是数据类型和变量名 :

int numberOfDays;

In the above example, a variable called "numberOfDays" has been declared with a data type of int. Notice how the line ends with a semi-colon. The semi-colon tells the Java compiler that the declaration is complete.

在上面的示例中,已声明一个名为“ numberOfDays”的变量,其数据类型为int。 注意,行以分号结尾。 分号告诉Java编译器声明已完成。

Now that it has been declared, numberOfDays can only ever hold values that match the definition of the data type (i.e., for an int data type the value can only be a whole number between -2,147,483,648 to 2,147,483,647).

现在已经声明了,numberOfDays只能保存与数据类型的定义匹配的值(即,对于int数据类型,该值只能是-2,147,483,648到2,147,483,647之间的整数)。

Declaring variables for other data types is exactly the same:

声明其他数据类型的变量完全相同:

byte nextInStream;
 short hour;
 long totalNumberOfStars;
 float reactionTime;
 double itemPrice;

( Initializing Variables )

Before a variable can be used it must be given an initial value. This is called initializing the variable. If we try to use a variable without first giving it a value:

在使用变量之前,必须给它一个初始值。 这称为初始化变量。 如果我们尝试使用变量而不先给它赋值:

int numberOfDays;
 //try and add 10 to the value of numberOfDays
 numberOfDays = numberOfDays + 10;
the compiler will throw an error:
 variable numberOfDays might not have been initialized

To initialize a variable we use an assignment statement. An assignment statement follows the same pattern as an equation in mathematics (e.g., 2 + 2 = 4). There is a left side of the equation, a right side and an equals sign (i.e., "=") in the middle. To give a variable a value, the left side is the name of the variable and the right side is the value:

要初始化变量,我们使用赋值语句。 赋值语句遵循与数学方程式相同的模式(例如2 + 2 = 4)。 等式的左边是一个,中间是一个等号(即“ =”)。 要给变量赋值,左侧是变量的名称,右侧是值:

int numberOfDays;
 numberOfDays = 7;

In the above example, numberOfDays has been declared with a data type of int and has been giving an initial value of 7. We can now add ten to the value of numberOfDays because it has been initialized:

在上面的示例中,numberOfDays的数据类型为int,并且初始值为7。我们现在可以将numberOfDays的值加10,因为它已被初始化:

int numberOfDays;
 numberOfDays = 7;
 numberOfDays = numberOfDays + 10;
 System.out.println(numberOfDays);

Typically, the initializing of a variable is done at the same time as its declaration:

通常,变量的初始化与变量的声明同时进行:

//declare the variable and give it a value all in one statement
 int numberOfDays = 7;

( Choosing Variable Names )

The name given to a variable is known as an identifier. As the term suggests, the way the compiler knows which variables it's dealing with is through the variable's name.

赋予变量的名称称为标识符。 就像术语所暗示的那样,编译器通过变量的名称来知道要处理的变量。

There are certain rules for identifiers:

标识符有一些规则:

  • reserved words cannot be used. 保留字不能使用。
  • they cannot start with a digit but digits can be used after the first character (e.g., name1, n2ame are valid).
  • they can start with a letter, an underscore (i.e., "_") or a dollar sign (i.e., "$").
  • you cannot use other symbols or spaces (e.g., "%","^","&","#").

Always give your variables meaningful identifiers. If a variable holds the price of a book, then call it something like "bookPrice". If each variable has a name that makes it clear what it's being used for, it will make finding errors in your programs a lot easier.

始终为变量提供有意义的标识符。 如果变量持有一本书的价格,则将其称为“ bookPrice”。 如果每个变量都有一个清楚说明其用途的名称,它将使在程序中查找错误变得容易得多。

Finally, there are naming conventions in Java that we would encourage you to use. You may have noticed that all the examples we have given follow a certain pattern. When more than one word is used in combination in a variable name the words following the first one are given a capital letter (e.g., reactionTime, numberOfDays.) This is known as mixed case and is the preferred choice for variable identifiers.

最后,我们鼓励您使用Java中的命名约定 。 您可能已经注意到,我们给出的所有示例都遵循一定的模式。 当在变量名称中组合使用多个单词时,第一个单词之后的单词将被赋予大写字母(例如,reactionTime,numberOfDays)。这被称为混合大小写,并且是变量标识符的首选。