The difference between basic data types and reference data types in Java

http://www.mamicode.com/info-detail-2380502.html

1. Basic data types

byte: The smallest data type in Java. It occupies 8 bits (bit) in the memory, that is, 1 byte. The value range is -128~127, and the default value is 0.

short: short integer, occupies 16 bits in the memory, that is, 2 bytes, the value range is -32768~32717, the default value is 0.

int: Integer, used to store integers. It occupies 32 bits internally, that is, 4 bytes. The value range is -2147483648~2147483647, and the default value is 0.

long: Long integer, occupying 64 bits in the memory, that is, 8 bytes -2^63~2^63-1, the default value is 0L.

float: floating-point type, occupying 32 bits in the memory, that is, 4 bytes, used to store numbers with decimal points (the difference from double is that the float type has only 6 to 7 effective decimal points), the default value is 0.

double: Double-precision floating-point type, used to store numbers with a decimal point. It occupies 64 bits in the memory, that is, 8 bytes, and the default value is 0.

char: Character type, used to store a single character, occupies 16 bits, that is, 2 bytes, the value range is 0~65535, and the default value is empty.

boolean: Boolean type, occupies 1 byte, used to judge true or false (only two values, namely true, false), the default value is false.

 

Two, reference data type

Class, interface type, array type, enumeration type, annotation type.

Three, the difference

When the basic data type is created, a memory is allocated to it on the stack, and the value is directly stored on the stack. E.g:

  var a = 10;

  var b = a;

  b = 20;

  console.log(a); // 10 value

b just saves the value of a. So the change of b value has no effect on a.

The following figure demonstrates the process of this basic data type assignment:

 

 

When a reference data type is created, first allocate a piece of memory for its reference on the stack, and the specific information of the object is stored in the heap memory, and then the reference on the stack points to the address of the object in the heap (Heap) . [Reference (stack) ——> Object address (heap)]

For example, there is a class Person with attributes name, age, and a constructor with parameters.

Person p = new Person("Tom", 20);

The specific creation process in memory is:

  1. First allocate a memory space for p in the stack memory;

  2. Allocate a space in the heap memory for the Person object, and set initial values ​​"", 0 for its two properties;

  3. According to the definition of attributes in the Person class, assign values ​​to the two attributes of the object;

  4. Call the constructor and assign the values ​​"Tom", 20 to the two properties; (note that there is no connection between p and the Person object at this time);

  5. Assign the address of the Person object in the heap memory to p in the stack; you can find the specific information of the object in the heap by referencing p.

 

Four, related (heap area, stack area, constant area, static area)

Heap area: Generally allocated and released by the programmer, the memory allocated by the malloc series of functions or the new operator, its life cycle is determined by free or delete. It exists until the program ends and is released by the OS.

Stack area: It is automatically allocated and released by the compiler to store local variables (basic data and object references, but the object itself is not stored in the stack, but in the heap). The content on the stack only exists in the scope of the function, when the function ends.

Constant area: Store constants, and the constant string (String) is placed here. Released by the system after the program ends.

Static area: 1. Also called method area, like heap, it is shared by all threads. 2. Store all class and static variables. 3. The method area contains the only elements that are always in the program. 4. The content of the static area exists throughout the life cycle of the program and is allocated by the compiler during compilation.

Case study-string memory allocation:

  For strings, the references to their objects are stored in the stack. If they have been created at compile time (defined directly with double quotes), they will be stored in the constant pool. It can only be determined at runtime (new out). Is stored in the heap. For equals strings, there is always only one copy in the constant pool and multiple copies in the heap. 

1 String s1 = "china"; 2 String s2 = "china"; 3 String s3 = "china"; 4 5 String ss1 = new String("china"); 6 String ss2 = new String("china"); 7 String ss3 = new String("china");

 

 

  Here is an explanation of the three yellow arrows. When a string is generated by new (assuming "china"), it will first go to the constant pool to find whether there is already a "china" object, if not, create one in the constant pool This string object, and then create a copy of the "china" object in the constant pool in the heap.

  This is also an interview question: Strings=newString("xyz"); How many objects are generated, one or two? If there is no "xyz" in the constant pool, there are two.

Guess you like

Origin blog.csdn.net/qq_34159161/article/details/105590192