There are 3 steps: declaration, instantiation and assignment.
int a[] = new int[]{1, 2, 3};
- step1:
int a[];
creates a 64-bit reference variable in the function stack for storing an address. - step2:
new int[]{1, 2, 3};
creates an array instance in the heap, then returns the base address of this instance. - step3: assign the address that step 2 returned to the variable a.
Objects can be lost if you lose the bits corresponding to the address. For example if the only copy of the address of a particular class is stored in x
, then x = null
will cause you to permanently lose this instance. This isn’t necessarily a bad thing, since you’ll often decide you’re done with an object, and thus it’s safe to simply throw away the reference.