Integer i3 = 10;Integer i4 = 10;if(i3 == i4) System.out.println("same object");if(i3.equals(i4)) System.out.println("meaningfully equal");This example produces the output:same objectmeaningfully equal
Originally posted by Ganesh MP:
I have just pasted the code from K&B Chap 3, P 235 and 236
I also wonder how its possible as given in the book!
Creating an object using "new" results in a different object reference than when creating an object by declaring it's type and using an assignment operator. I'm still a bit fuzzy on this one since the values are less than 127 and I would have thought that the rule on page 236 would apply here.
Following this discussion the answer of Q.2 in the Self Test in the same chapter 3 confused me. I thought that the CardBoard object (c1) gets garbage collected but its associated Short wrapper object stays alive because there still is another CardBoard object, which has an associated Short wrapper with the same primitive value (5). And if both instance variables refer to the same object from the pool, disappearing of one of the variables shouldn't make the object eligible for GC.
Please, if anyone did this question say what he/she thinks
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
if(c1.story==c2.story){System.out.println("Same reference");}
//c1 = null;
// do Stuff
} }
2. Given:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.
Answer:
� 3 C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
�˚ A, B, D, E, and F are incorrect based on the above. (Objective 7.4)
Originally posted by Ganesh MP:
read it is fundda of byte poll
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Consider Paul's rocket mass heater. |