There are two main types of data in Java, basic data types and reference data types.
Basic data type:
byte: Smallest data type in Java, 8 bits in memory (bit), 1 bytes, value range -128~127, default value 0
short: Shorter integer, 16 bits in memory, 2 bytes, range -32768~32717, default value 0
int: integer, for storing integers, 32 bits in memory, 4 bytes, value range -2147483648~2147483647, default value 0
Long: integer, 64 bits in memory, 8 bytes -2^63~2^63-1, default 0L
float: floating point, 32 bits in memory, 4 bytes, for storing a number with a decimal point (the difference from a double is that the float is a valid decimal point only 6~7 bit), and the default value of 0
Double: dual-precision floating point, used to store a number with a decimal point, 64 bits in memory, 8 bytes, default value 0
Char: Character type, used to store a single character, 16 bits, or 2 bytes, value range 0~65535, default value is empty
Boolean: boolean type, 1 bytes, used to determine true or false (only two values, that is, true, false), default value False
The basic data types are stored directly in memory on the memory stack, the value of the data itself is stored in the stack space
Reference types that inherit from the object class (also reference types) are stored according to the memory model of the stored object in Java, using the Java memory heap and memory stack for this type of data storage, and simply saying that "references" are stored on an ordered memory stack, And the value of the object itself is stored on the memory heap;
whether it is a basic data type or a reference type, they will first allocate a piece of memory on the stack, for the base type, the area contains the basic type of content, and for the object type, this area contains a pointer to the real content, the real content is manually allocated to the heap .
some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.
heap memory is used to hold objects and arrays created by new . The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the array or the first address of the object in the heap memory, and this particular variable in the stack becomes the reference variable of the array or object. You can then use the reference variable in the stack memory in your program to access the array or object in the heap, which is the equivalent of an alias, or codename, of an array or object.
A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses the new generation of arrays and objects, the heap memory that the array and the object itself occupies will not be freed, and arrays and objects become garbage, no longer used, but still occupy memory when no reference variable points to it. is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!
Data types in Java