Object and reference, Object Reference
Objects and references
Let's look at this example first:
Int [] a = {5, 5}; int [] B = a; B [1] = 45;
The output value of array a is as follows: 5, 45, 5
This is because a and B point to the same object {5, 5}. both a and B are referred to as references to this object, that is, a and B operate on the same object.
Empty object
Int [] a = {5, 5}; int [] B = a; a = null;
Set a to null, which means that a will not point to any object.
As a resultNull Pointer Error NullPointerException
As follows:
Int [] a = {11,15, 14}; a = null; a [0] = 12; // Error
If a is set to null, it will not point to any object, and it will not be able to access a [0].
The referenced object is lost.
Int [] a = {8, 8}; a = new int [4];
A originally pointed to the object {8, 8}, and then a was set to point to the object int [4], so {8, 8} was not referenced, that is, if the referenced object is lost, it will be recycled and deleted by the java recycle mechanism.