Click here to Skip to main content
15,302,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am learning classes and objects in Java. I am currently learning about the creation of objects. However, in the Instantiation step what do these two lines mean?

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.

The new operator returns a reference to the object is created.

What does returning a reference to that memory mean in statement 1?

What does reference in statement 2 mean? Is it the memory address of the object that is stored in heap memory?

Please check the below documentation
https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

What I have tried:

I researched on the internet and found out that reference is an address that indicates where an object's variables and methods are stored. However, I wanted to confirm whether this is true or not.
Posted
Updated 17-Jan-22 0:19am
v2

1. The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.

The new operator instantiates means when we create an object of class it allocate space on memory to store
the data of respective objects return an address or reference of memory location where data resides.
Example:

public class Math {
  int x = 5;
  
  public int Add(int no1, int no2) {
      return no1 + no2;
   }
}

public static void main(String[] args) {
    Math myObj = new Math();
    System.out.println(myObj.x);
  }


Math myObj = new Math() statement is initialization and myObj is an object of Math class. 

2. The new operator returns a reference to the object is created.

In above example 'new Math();' is initialization which will return the reference or address of memory.
We will get all data members (x) and methods (Add).
   
Comments
Sourabh Chavan 17-Jan-22 6:30am
   
Final doubt

So the reference variable myObj containing the reference to object points to the memory location(memory address) instead of storing the actual memory location(memory address)

Right?
M Imran Ansari 17-Jan-22 7:25am
   
Yes. myObj is just a reference (hashCode is a randomly generated number and has nothing to do with the address) of memory location not the actual/physical address (physical address is not accessible)
Quote:
Is it the memory address of the object that is stored in heap memory?
Not exactly (even if, roughly speaking, it 'serves the same purpose'), see, for instance: How can I get the memory location of a object in java? - Stack Overflow[^].
   
Comments
Sourabh Chavan 17-Jan-22 6:45am
   
Final doubt

So the reference variable myObj containing the reference to object points to the memory location(memory address) instead of storing the actual memory location(memory address)

Right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900