Forgive me if this is a duplicate, I can't seem to find anything that explains what I'm seeing well.
The following program:
Object a = new Object();
Object b = a;
System.out.println( "a: " + a );
System.out.println( "b: " + b );
a = null;
System.out.println( "a: " + a );
System.out.println( "b: " + b );
Yields this:
a: java.lang.Object@3e25a5
b: java.lang.Object@3e25a5
a: null
b: java.lang.Object@3e25a5
But WHY?!? I'm so confused by this. Shouldn't "b" be referencing "a"? Therefore, if "a" no longer references anything else (eg: null) then shouldn't "b"? I'm obviously missing something fundamental here.
Thanks in advance.
EDIT #1
I think what threw me off was I am printing out the address. For some reason, in my mind, I was printing out some magic value indicating the pointers\references - when in reality set开发者_StackOverflow中文版ting b = a
is not making them the same, it is simply creating a new pointer to the same place on the heap. I hope this helps someone else.
b
is not referencing a
it referencing to the same object a
is referencing to when the assignment was performed.
I know this is not same language but if you are familiar with C, it is like:
int someNum = 5;
int * a = &someNum; // a points to someNum
int * b = a; // b points to someNum too
a = NULL; // a doesn't point to anything (points to address 0), b still points to someNum
(I used C as it gives a clearer look of addresses and references)
Lets take a detailed look at what is happening:
//A is pointing to a new Object lets assume memory location for 1 for simplicity
Object a = new Object();
//B is now also pointing to memory location 1
Object b = a;
//Both print the same they are both pointing to memory location 1
System.out.println( "a: " + a );
System.out.println( "b: " + b );
//a is now pointing to null, but there is no change to b which is still pointing to memory location 1
a = null;
System.out.println( "a: " + a );
System.out.println( "b: " + b );
Java always passes-by-value, that is all.
Object a = new Object();
This line creates a new object on the heap, and puts a reference in the variable a
.
Object b = a;
Java is pass-by-value, so this line passes/assigns the value of that reference into the variable b
.
a = null;
This means the variable a
should no longer point to that place on the heap, but should point nowhere. But it shouldn't mean that b
points nowhere, since we passed the reference by value, so b
is still pointing to that Object on the heap.
The key observation here is pass-by-value. Since essentially we photocopied a
's value into b
, changing the original won't have an effect on how the photocopy looks since it's already taken.
Both a and b are references to Object instance you created. It means that both point to the same memory area. Then you destroy one of these references. Now a points to null. But both object and the second reference to it b are alive. This is what you see.
a references a new Object (Object a = new Object()
)
b references the same Object as a (Object b = a
)
a references nothing (a=null
)
b still references the Object
null
is not premitive and it's considered as object, so when you call a = null
, a
will refer to nothing while b
still refering to the same object new Object()
精彩评论