Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What's the difference between variables and constants in Java?
What's the difference between variables and constants in Java?
There are a lot of data in the program to represent the state of the program. Some data values will change, while others will not. These data are called variables and constants respectively in the program. The following small series tells you the difference between variables and constants in Java. Let's have a look.

The difference between variables and constants in Java

Constant: The value of the constant remains unchanged.

Grammar:

Data type constant name = value;

Bipi = 3.14;

Remarks:

Usually, the default constant name is capitalized.

The relationship between variables and constants (the relationship between quantities)

Let's first understand the relationship between variables and constants in Java from a simple example.

The following program declares two variables that Java often uses, namely integer variable num and character variable ch. After assigning values to them, display their values on the console respectively:

The following program declares two variables, one is an integer and the other is a character.

Output result:

3 is an integer!

Z is a character!

Description:

Two different types of variables num and ch are declared, and the constant 3 and the character "z" are assigned to these two variables respectively, and then displayed on the display. When a variable is declared, the compiler will open a memory space enough to accommodate the variable in memory. No matter how the value of the variable changes, the same memory space is always used. Therefore, making good use of variables will be a way to save memory.

Constants are different from variables and their values are fixed, such as integer constants and string constants. Usually, when assigning a value to a variable, a constant is assigned to it. In the TestJava program, line 6 num is an integer variable, and 3 is a constant. The function of this line of code is to declare num as an integer variable and assign the value of constant 3 to it.

Similarly, line 7 declares a character variable ch and assigns the character constant' z' to it. Of course, you can reassign variables or use declared variables while the program is running.

Variable introduction

Variables represent the state of the program. A program changes the state of the whole program by changing the value of a variable, or to put it a little bigger, it is to realize the functional logic of the program.

In order to reference the value of a variable conveniently, it is necessary to set a name for the variable in the program, which is the variable name. For example, in a 2D game program, if you need to represent the position of a character, you need two variables, one is the X coordinate and the other is the Y coordinate, and the values of these two variables will change during the running of the program.

Because the Java language is a strongly typed language, variables must be declared before use. The syntax format of declaring variables in the program is as follows

Data type variable name;

For example: int x;;

In this syntax format, the data type can be any type in the Java language, including the basic data type introduced earlier and the compound data type to be introduced later. The name of the variable is the identifier of the variable, which needs to conform to the naming rules of the identifier. In practical use, the name generally corresponds to the purpose of the variable, which is convenient for the program to read.

Use spaces between data types and variable names. There is no limit to the number of spaces, but at least 1 is required. Statement uses';' As an end.

You can also set the value of a declared variable in the following syntax format.

Data type variable name = value;

For example: int x =10;

In this syntax format, the previous syntax is the same as that described above, and the following "=" represents assignment, where "value" represents specific data. In this syntax format, it is required that the type of the value should be consistent with the data type of the declared variable.

You can also declare multiple variables of the same type at a time, and the syntax format is as follows: data type variable name 1, variable name 2, … variable name n; For example: int x, y, z; In this syntax format, variable names are separated by ",",and there can be any number of variable names here.

When declaring multiple variables, you can also assign values to variables. The syntax format is as follows

Data type variable name 1= value 1, variable name 2= value 2, … variable name n= value n;

For example:

int x = 10,y=20,z = 40

You can also selectively assign values when declaring variables, such as int x, y= 10, z; In the above syntax format, if multiple variables are declared at the same time, the types of these variables must be the same. If the declared variables are different, they only need to be declared separately, for example

int n = 3;

Boolean b = true;

char c;

In a program, the value of a variable represents the state of the program. In a program, values stored in variables can be referenced by variable names, and variables can be reassigned. For example:

int n = 5;

n = 10;

In the actual development process, what kind of variables need to be declared, how many variables need to be declared, and what values need to be assigned to variables are all determined according to the program logic. What is listed here is only the expression format.

Constant introduction

Constants represent values that cannot be changed while the program is running.

Constants have two main functions in the process of program running.

They are representative constants, which are convenient to modify the program and enhance the readability of the program.

For the syntax format of constant and variable types, just add the keyword final before the syntax format of variables. In the Java coding specification, it is required that constant names must be capitalized.

The syntax format of this constant is as follows

Final data type constant name = value;

Final data type constant name 1 = value 1, constant name 2 = value 2, ... constant name n = value n;

For example:

The final double pi = 3.14;

The final characters are male ='M' and female =' f.

In Java syntax, constants can also be declared first and then assigned, but only once. The sample code is as follows:

Eventually int UP

UP = 1;

Sample codes corresponding to two uses of constants are as follows:

Representative constant

The final double pi = 3.14;

int r = 5;

double l = 2 * PI * r;

double s = PI * r * r

In this sample code, the constant PI represents the mathematical value ∏, that is, pi, which is a mathematical constant, and the following variable r represents the radius, L represents the circumference of the circle, and S represents the area of the circle.

If you need to increase the calculation accuracy of the program, just change the value of pi from 3. 14 to 3. 14 15926, recompile the program, and the subsequent values will automatically change, making the code easy to modify and maintain.

Enhance the readability of the program

Int direction;

Eventually int up =1;

final int DOWN = 2;

final int LEFT = 3;

final int RIGHT = 4;

In this sample code, the variable direction represents the value of the direction, and the following four constants, UP, DOWN, LEFT and RIGHT, represent up, down, left and right. Their values are 1, 2, 3 and 4 respectively, which can improve the readability of the program when reading.