type of data
Basic data type
Reference data type
Data type conversion
The difference between the two
1. From a conceptual point of view, the
basic data type: variable name points to a specific value.
Reference data type: variable name points to the memory address of the data object, that is, the variable name points to the hash value
Second, from the aspect of memory construction, the
basic data type: java will allocate his memory space immediately after the variable is declared
int a; //声明变量a的同时,系统给a分配了空间
a=12;
Reference data type: It points to the object entity (specific value) in a special way (similar to a C pointer). This type of variable declaration does not allocate memory, but only stores a memory address.
MyDate today;
today.day = 14; //错误!因为today对象的数据空间未分配
MyDate today;
today = new Date(); //正确的方式
3. From the aspect of assignment, the
basic data type: need to assign a specific value when in use, b = a; the value of a is passed to b, and it is stored in the memory space of b. Use the "==" sign to
refer to the data type when judging: you can assign null when you use it, and use the equals method when judging, b = a; it is to pass the address of the data of a to b, that is, both a and b store an address , This address points to the actual data. (Similar to pointers in C++)
other
Use "—" to divide the value
When there are many numerical digits used in the program, the binary number of the digits is to prevent programmers from being blind to the digits of the numerical value. Java7 introduces a new function to divide the value with "_"
//定义一个32位的二进制数,最高位是符号位
int binVal = 0B1000_0000_0000_0000_0000_0000_0000_0011;
double pi = 3.14_15_92_65_36;
Use var to define variables
Var is equivalent to a dynamic variable. The type of a local variable defined using var is automatically inferred by the compiler ---- assigned when the variable is defined, then the variable is the initial value of what type and what type
Note: Java var cannot be used in method return values and member variables!
public void test() {
var str = "这是一个字符串类型的数据"; // 等价于 String str = "这是一个字符串类型的数据";
System.out.println(str); // java编译器会自动根据str变量右边的数据类型来匹配
}