3
public class Box {

    int height;
    int length;
    int width;

}



public class Volume {

    public static void main(String args[]){

        Box mybox = new Box();
        Box mybox1= mybox;
        Box mybox2 = new Box();


        System.out.println(" The address of the mybox object is:"+System.identityHashCode(mybox));
        System.out.println(" The value of the mybox object is:"+mybox);

        System.out.println(" The address of the object mybox1 is :"+ System.identityHashCode(mybox1));
        System.out.println(" The value of the mybox1 object is:"+mybox1);


        System.out.println(" The address of the object mybox2 is :"+ System.identityHashCode(mybox2));
        System.out.println(" The value of the mybox2 object is:"+mybox2);

    }
}

Goal of this program is to help me understand the concepts of Object creation and memory allocation in Java. Based on my understanding, the new operator shall create a memory for an object and return the reference stored in the memory. In order to get the address of the object, I am using the System.identityHashCode function. In order to get the value of the reference stored in that memory address, I am directly printing the object. Below we can see the results.

 The address of the mybox object is:1956725890

 The value of the mybox object is:Box@74a14482

 The address of the object mybox1 is :1956725890

 The value of the mybox1 object is:Box@74a14482

 The address of the object mybox2 is :356573597

 The value of the mybox2 object is:Box@1540e19d

Based on the above results, my understanding is that mybox and mybox1 will have the same address and they will carry the same reference value in that address as mybox1 was not instantiated and was assigned to mybox. mybox2 shall have a new address and it will have a new reference value in that address.

So my question is

  • Without instantiating, how would mybox1 have an address and a reference in that address ?

  • The book Java for complete reference does mention that every object after instantiation shall hold the memory address of the actual object Box.So why is the value of the reference stored in the mybox2 different from the value of the reference stored in mybox ? I am assuming that just by printing the variable, it would give me the value stored in that variable, which is the reference to the object Box.

Thank you people for any suggestions that can help me understand the very basic concepts of java. I am a newbie and I really appreciate the wealth of knowledge spread by stackoverflow members.

user207421
  • 305,947
  • 44
  • 307
  • 483
Aryans
  • 31
  • 1
  • 5
  • without instantiating? what do you think: :mybox2 = new Box(); does? – Stultuske Jan 05 '16 at 11:28
  • The first thing you need to understand is that `mybox` is a variable, not an object. The value of that variable is a reference, not an object. See http://stackoverflow.com/questions/32010172 – Jon Skeet Jan 05 '16 at 11:28
  • I am sorry, my question 1 should have referred to mybox1. I am new to this stackoverflow and I am trying to edit this question. Thanks for your prompt response. – Aryans Jan 05 '16 at 11:33
  • The statement `Box mybox1 = mybox;` does not instantiate, as I think you understand. It assigns the reference value of mybox to the variable mybox1. The object represented there now has two references, one in mybox and one in mybox1. If either of those references were used to change a value within that object, it would also change for the other reference since the two variables refer to the same object. – arcy Jan 05 '16 at 11:55
  • `System.identityHashCode()` does not return an address. – user207421 Jun 10 '16 at 04:38

4 Answers4

1

a) The line Box mybox1= mybox; makes the variable mybox1 point to the Object created by the mybox variable. Hence, both the variables mybox and mybox1 point to the same Object. Note that new operator is used only once in the first 2 lines.

b) Since you are creating a new Object for mybox2 in the line Box mybox2 = new Box();, it will create a fresh new object in the heap. The values of the variables mybox and mybox2 you see are basically the name of the class followed by hashcode of the object in hexadecimal format.

When you print the variable mybox using System.out.println() it internally executes the toString method:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Community
  • 1
  • 1
Sameer Mirji
  • 2,135
  • 16
  • 28
1

a) myBox1 is pointing to myBox that's why when you printed its reference, it was the same as myBox. The following might help you understand:

Box myBox;

We just created an instance variable of type Box in the stack, pointing to null.

myBox = new Box();

The new operator allocated an object on the heap and returned its pointer to myBox.

Box myBox1 = myBox;

We created another instance variable on the stack that is pointing to the same reference that myBox is pointing to.

Now if we manipulated myBox1 and tried to access myBox like:

myBox1.height = 1;
System.out.println(myBox.height);

The output should be 1, as both myBox and myBox1 are pointing to the same reference.

b) Since the 2 instance variables myBox2 and mybox are pointing to different objects in memory, according to this answer the identifyhashcode method might return different value as it might depend on the reference the object is at.

That integer returned by identityHashCode may be related to the (a) machine address for the object, or it may not be. The value returned by identityHashCode() is guaranteed not to change for the lifetime of the object.

Community
  • 1
  • 1
Essam Ewaisha
  • 439
  • 5
  • 15
1

It's ANALOGY time!

You can imagine a variable (mybox, mybox1 and mybox2) are children. And they can hold balloons using strings (not be confused with the String in Java, I mean actual strings). The balloons are objects and the strings are references.

In the first line of code, mybox is holding a string that connects to the balloon at 1956725890. Now in line 2, mybox1 also takes a string and connects it to the balloon at 1956725890. That's what this code does,

Box mybox1 = mybox;

In the third line of the code,

Box mybox2 = new Box();

Another child, takes a string and connects it to another box, not the one at 1956725890.

For your first question, the answer is,

That's just how it is.

As I have said above, the second line of the code copies the reference. The point is, no new objects are created, just a new reference. So yeah, no instantiation.

For your second question, I don't really understand what you are asking here. Because of course mybox holds a different reference, and therefore a different object from mybox2.

As I said before, line 3 is creating another object and creating a new reference to that object. So why should they be the same?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
-1

The variables mybox and mybox2 contain references to two different objects.

Box a=new Box();

a is a reference to an object

Box b;

b is a reference to nothing null

So the keyword "new" creates a new object and stores it.

The address of this object is stored in the variable.

user207421
  • 305,947
  • 44
  • 307
  • 483
Mark
  • 47
  • 2
  • 13