Reputation: 1837
I'm reading this book on data structures and it covers memory management and orphaned objects in Java.The textbook says the following:
For example, consider the three assignment statements in the figure at left. After the third assignment statement, not only do a and b refer to the same Date object (1/1/2011), but also there is no longer a reference to the Date object that was created and used to initialize b. The only reference to that object was in the variable b, and this reference was overwritten by the assignment, so there is no way to refer to the object again. Such an object is said to be orphaned.
Code:
Date a=new Date(12, 31, 1999);
Date b=new Date(1, 1, 2011);
b=a;
Is that statement true? Shouldn't the reference of a (the memory location of object Date(12, 31, 1999)
be what the reference of b be? This seems like one huge error but there is even a picture showing memory block for 12, 31, 1999 being the orphaned object.
Picture: http://imageshack.us/f/818/3tkx.jpg/
Upvotes: 3
Views: 243
Reputation: 200256
You are right, the orphaned object is the one which was pointed to by b
, which is Date(1, 1, 2011)
. If the picture shows otherwise, then it is definitely wrong.
a --> 31.12.1999 b --> 1.1.2011
|
\ /
a --> 31.12.1999 <-- b 1.1.2011 (orphaned!)
Upvotes: 2
Reputation: 1455
Yep, the
(1/1/2011)
in the text is wrong. It's most probable a editorial error. Apart from that everything else is correct. b is overwritten with the content of a, which is Date(12, 31, 1999). Therefore both variables point to Date(12, 31, 1999) and Date(1, 1, 2011) is orphaned and can be garbage collected.
Maybe you should check an errata of your book. :-)
Upvotes: 2
Reputation: 30568
In java you always assign something on the right to a reference to the left.
So you statements say something like this:
Date
object Date(12, 31, 1999)
to the variable a
Date
object Date(91, 1, 2011)
to the variable b
a
to variable b
.So it looks like this if I follow those steps:
a
-> Date(12, 31, 1999)
a
-> Date(12, 31, 1999)
b
-> Date(1, 1, 2011)
a
-> Date(12, 31, 1999)
b
-> Date(12, 31, 1999)
Please note that after this assignment the original object of Date(1, 1, 2011)
is no longer referenced since you cannot reach it from your application. Its original referencing variable b
is overwritten and now the object Date(12, 31, 1999)
is referenced from both a
and b
. Date(91, 1, 2011)
is orphaned and ready to be garbage collected.
Imagine this as if you were holding a sword and an axe. First you pick up a sword. Then you pick up the axe. After that you drop the sword and drag the axe you still have in your hand with both hands. After that you do not hold the sword you dropped (it is lost).
Edit: This is an error if you tell the author of the book he will be grateful.
Upvotes: 7
Reputation: 13792
The sentence is false (the explanation is fine, but he confuses the objects)
new Date(12, 31, 1999)
is referenced two times, by a
and b
new Date(91, 1, 2011)
is not referenced by anyone else after the b=a
assigment, so it will be deleted from memoryUpvotes: 1
Reputation: 9741
Lets say a
's referring to Date object stored at memory location 123
.
Going by same logic, lets say b
's referring to Date object stored at memory location 546
.(New object new location).
Now when you do b = a
. b
's referring to Object @ 123
. 546
is no longer accessible and can be GC'ed anytime.
Upvotes: 2
Reputation: 17007
The b=a
statement discards the current value of b
, and since there are no other references to the date object that was called b, yes, the b date is orphaned.
Upvotes: 2