This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Autoboxing vs Unboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Autoboxing vs Unboxing" Watch "Autoboxing vs Unboxing" New topic
Author

Autoboxing vs Unboxing

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

Please go throug the code below :


I have a basic question on boxing and unboxing of the wrapper classes. From the above code,
I understand that from line#1 b1==b2 resulted false, as b2 is pointing to a completely new object on heap and b1 is pointing to a reference (from boolean pool, just like string pool) to another diferent object on the heap.
On line # 2, b1==b3 , resulted true, because I guess b3 is autoboxed to a Boolean object and both are pointing to a same boolean reference from boolean pool. Or considering other way b1 might be unboxed becoming a boolean identifier and now both b1 and b3 are booleans not Booleans. So since both holding same bit pattern, resulted true
My question is regarding line # 3, I believed that this should result in false, assuming b3 getting autoboxed to Boolean object, it would point to an entirely a different object (again some reference from boolean pool), from that one being pointed by b2. But the answer is true. Or did b2 got Unboxed itself

How could the line # 3 resulted in true? What actually happened here? Did unboxing happened here or autoboxing? Which one would take priority? How to decide which one is going to happen?

Please clarify me on this. I have the same query on the Integer operation also, given in the code and believe the same postulate holds good for other wrapper classes.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

At line 2 auto unboxing happened to object 'i1' same as i1.intVaue() Same at line 3, this when using == and if it were going about objects got boxed and refer to each other in the pool, then try bigger value(more than 127).
When using equals() auto boxing takes place as in line 6.
[ September 27, 2007: Message edited by: Ahmed Yehia ]
Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
boolean pool? whats a boolean pool?


I want to be like marc
Per Radfe
Greenhorn

Joined: Jul 05, 2007
Posts: 14
Wrappers are imutable, just like strings. So, they got a pool so they can be reused to save memory. (Correct me if i�m wrong :roll: )
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

Originally posted by Chandra Kota:

...How could the line # 3 resulted in true? What actually happened here? Did unboxing happened here or autoboxing? Which one would take priority? How to decide which one is going to happen? [banghead]...


In line #3 the Boolean b2 will unboxed to primitive boolean an then the check will happen. The priority is simple, when there is both wrapper and primitive present then the wrapper will unboxed...


MooN
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
Originally posted by Per Radfe:
Wrappers are imutable, just like strings. So, they got a pool so they can be reused to save memory. (Correct me if i�m wrong :roll: )


I agree with Per but I could be wrong so could someone please confirm the above.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
From Ahmed's update

At line 2 auto unboxing happened to object 'i1' same as i1.intVaue() Same at line 3, this when using == and if it were going about objects got boxed and refer to each other in the pool, then try bigger value(more than 127).
When using equals() auto boxing takes place as in line 6.


Does this mean that if we use "==" auto Unboxing happens and when we use "equals" autoboxing happens. Please confirm on this.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Autoboxing vs Unboxing
 
Similar Threads
Small doubt in Boxing & == & equals()
Boolean values
Integer's == and !=
From mock exam about Autoboxing
Output of this code snippet