As a JAVA development engineer, do you know the basic knowledge about variables?

Variables are basically necessary in the development process, so do you really have a solid grasp of some of his basic common sense? This article mainly explains the small common sense of variables in detail, friends who need it, let’s learn together with the editor.

1. What is a variable?

Variables are essentially a space in memory. Variables have four basic attributes: variable name, data type, storage unit and variable value

Variable name: legal identifier
Data type: can be a basic type or a reference type
Storage unit: The size of a storage unit is determined by the data type, such as: int is 4 bytes, 32-bit
variable value: put in the storage unit Is the variable value (if it is a basic type, it is a specific value, if it is a
reference type , it is a memory address, if it is null, it means that it does not point to any object)

To be precise, it consists of three parts: data type, name, literal value (data)

Variable declaration format: type variable name;

Declaration and assignment can be done together.
int i = 0;

Second, the role of data types?

Different data has different types, and different data types will allocate different sizes of space at the bottom.
The data type is to guide how much memory space should be allocated during the running phase of the program.

JAVA data types are divided into 8 types: byte, short, int, long, float, double, char, boolean

Three, the role of variables?

It is similar to a hotel. I can quickly get the guest information based on the room name, and it can be used in multiple places. If this person is no longer available, I can let others live without having to rebuild a room.

Straightforward to say a statement repeated use effects. You can change the value of the variable at will.

Insert picture description here

Four, about the scope of variables

Scope Some beginners in Java may not have a very clear definition of this term. To put it bluntly, the place where it can be used is called scope. For example, the scope of the trash can is to throw garbage, and it is useless.

The scope of the variable is to describe the effective scope of the variable, in which scope the variable can be used. Here we only need to remember one sentence, and we wo n’t recognize it after the braces .

Here I define a variable in another method other than the main method, which cannot be used in the main method. Here we will find that it reports errors even during the compilation phase, let alone the runtime.

Five, about the classification of variables

According to the location of the variable, it is divided into two types, one is a local variable , and the other is a member variable

Local variables: The variables declared in the method body are called local variables.
Member variables: The variables declared outside the method body (inside the class) are called member variables

In this code, age is a member, and i is a local

Six, declare variables need to pay attention to

1. The problem of naming between scopes

In the same scope, the variable name cannot be repeated, but the variable can be reassigned.
In different scopes, the variable name can be the same.

It can be seen here that I have defined two variables with the same name, one is a member variable and the other is a local variable. It can still run normally.

2. Variable initialization problem

Variables in java must be declared and then assigned before they can be accessed.

In the following cases, errors will be reported. Errors reported during operation refer to no initialization. Some beginners may often hear about initializing the container. Various initializations are not very standard for the definition of initialization. If there is no initialization, it means that it is not in the memory. Open up space, so you can't use it.

3. Declare multiple in one line

It is allowed to declare multiple variables, but it should be noted that his assignment will only assign the last variable. Take the following code as an example:

It can be seen that there is no error when the statement is declared, and an error is reported when the output a is because it is not initialized, but the output c is OK.

Seven, the principle of JAVA proximity

Java code follows the principle of execution from top to bottom, and it follows the principle of proximity, which can also be extracted from variables.

It can be seen from this that what he outputs is not the variable declared by static, which is his principle of proximity.



8. What is a constant

Unlike variables, the quantity that does not change during the running of the program is called a constant. A constant can only be assigned once in the entire program. If a constant is assigned multiple times, a compilation error will occur.
To declare a constant in java, in addition to specifying the data type, it also needs to be qualified by the final keyword.

Declare a constant and assign a value to the constant, the code is as follows:

final double PI = 3.1415926;

Nine, difficult questions and answers

Must variables in Java be initialized?

Variables in Java include member variables and local variables. After member variables are declared, the Java virtual machine will assign default values ​​to them regardless of whether they are initialized. For example, the default value of int is 0. Local variables will not be assigned default values. At this time we have to initialize.

The difference between variables and constants

Constant is the amount that cannot be modified during the running of the program. In Java, final is used to define constants, and constants generally use capital letters. The value of the variable is allowed to be changed during the running of the program, it must be assigned or initialized before use, and stored in the heap or stack. A stack is a data structure in which data items are arranged in sequence, and data items can only be inserted and deleted at one end.

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/111828828