Java learning road-variables

Java learning road-variables

1. Definition

1. What is a variable?

In the computer, most of the data that needs to be processed in real time is stored in the memory . Inside the memory, several data storage units are divided, and each unit can store an 8-bit data, just like the same building contains many residential houses. In order to distinguish these storage units, each unit is assigned a number, which is called a memory address .

The essence of a variable is a memory space allocated by the computer according to the data type and size of the variable we need to use. This memory space contains one or more data storage units. This space contains the variable type, name and value.

When we write a Java program, we are operating on variables on the surface, but actually operating on data storage units in the memory space. It is just because of the compiler's credit that different variables can be combined with the corresponding memory addresses, and let us manipulate the data storage unit in the form of operating variables.

2. What is the data type?

Different data has different types, different types have different space allocation methods, and different allocation methods require different data spaces.

For example, in Java, an int and a float is four bytes, but float type but larger than the numerical range of numerical ranges int .

This is because the two types of data use space in different ways:

  • **int type: **The first bit is the sign bit for positive and negative, and the remaining 31 bits represent the value bit. It means that the data is obtained by directly adding the values ​​represented by each bit, so the value range represented is-2^31 ~ 2^31 - 1
  • **Float type: **The first bit is the sign bit to represent positive and negative, and the rest are eight exponent bits and 23 base bits (the base is unsigned). This type is an 8-bit exponent and 23-bit base, so that the maximum value that can be represented is the 2^23^127minimum value -2^23^127. So the scope is-2^23^127 ~ 2^23^127

The fundamental function of the data type is to tell the computer how much memory space should be allocated for this variable!

3. Variable declaration and assignment

int num;    // 声明一个整型的变量,变量名为num
num = 100;  // 为该整型变量赋值100
// 可以同时完成变量的声明与赋值
int num1 = 100;

Two, scope

1. Scope of scope

In Java programs, the use of variables is limited. This scope is called the scope of the variable.

A pair of "{}" is a scope. In the same scope, the variable name cannot be the same, but the variable can be reassigned.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int b = 10;

        for (int i=0 ; i<1 ; i++) {
    
    
            System.out.println(b);
        }

        // System.out.println(i);  此处会报错
    }
}

In the above program, "variable b" is defined in the "main method", and "for statement" is also in the "main method", so "variable b" can be accessed in the loop;

But the "variable i" is defined in the "for statement", so it can only be accessed in the "for statement". The variable has been released after the loop ends, and the "i variable" cannot be accessed outside the loop.

Therefore, in the process of writing a program, we can define variables inside of some variables that are only used in loops, judgments, etc. This will help us save memory space.

2. Member variables and local variables

  • Local variables : variables declared in the method body
  • Member variables : outside the method, the variables declared in the class body are called member variables
public class Demo {
    
    
    // 成员变量
    int a = 10;    

    public static void main(String[] args) {
    
    
        // 局部变量
        int a = 100;  
        int b = 1000;

        System.out.println(a);  // 100
        // 访问变量时遵循就近原则,优先访问局部变量
    }
}

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/109996541