Java Language Reference Data Types Instantiating a reference type

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Object obj = new Object(); // Note the 'new' keyword

Where:

  • Object is a reference type.
  • obj is the variable in which to store the new reference.
  • Object() is the call to a constructor of Object.

What happens:

  • Space in memory is allocated for the object.
  • The constructor Object() is called to initialize that memory space.
  • The memory address is stored in obj, so that it references the newly created object.

This is different from primitives:

int i = 10;

Where the actual value 10 is stored in i.



Got any Java Language Question?