Reference data types:

Class, interface type, array type, enum type, annotation type;

 

The difference between base data types and reference data types:

When a basic data type is created, it is allocated a block of memory on the stack, and the value is stored directly on the stack.

When a reference data type is created, it first allocates a block of memory on the stack for its reference (handle). The specific information of the object is stored in the heap memory, and the reference on the stack points to the address of the object in the heap.

For example, if you have a class Person that has attributes name,age, constructors with parameters,

Person p = new Person(“Tom”,20);

The specific creation process in memory is as follows:

1. Allocate a space for p in stack memory;

2. Allocate a block of heap memory for the Person object and set its three attributes to initial values “”, 0;

3. Assign two attributes of the object according to the definition of attributes in class Person;

4. Call the constructor and assign the two attributes to “Tom”,20; (Note that there is no connection between p and the Person object at this point);

5. Assign the address of the Person object in the heap to p in the stack; Handle P is used to find specific information about objects in the heap