Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing vs Unboxing

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean pool? whats a boolean pool?
 
Per Radfe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Gaurav Arora
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic