• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Autoboxing vs Unboxing

 
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.
 
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 ]
 
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean pool? whats a boolean pool?
 
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: )
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • 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...
 
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.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic