• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple doubt - Please help !!

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's say

Byte b=20;

if(b==20){
// something
}

/*
in statement b==20, b will be unboxed to byte or 20 will be boxed to Byte
*/

please explain.

Regards,

Abdul Mohsin
[ May 10, 2007: Message edited by: Abdul Mohsin ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Byte b=20;

if(b==20){
// something
}

/*
in statement b==20, b will be unboxed to byte or 20 will be boxed to Byte
*/

please explain.



Definitely primitive is not going to be boxed before comparison. b will be unboxed before comparison. It is the only way keep this rule OK. ==
comparison results true if one wrapper and another primitive comprises same value. And this comparison is done after unboxing the wrapper. This will always result true.





Thanks,
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Boxing 20 would always return false, as you'd compare two different Byte references.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra and Satou for your help !!!

Abdul Mohsin
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
I agree. Boxing 20 would always return false, as you'd compare two different Byte references.



Ah, but IF it was boxing a value in the range [-128..127] it would be caching the objects, wouldn't it? So you would get true in this case.

But it doesn't box, it unboxes. The rules are in the Java Language Specification.
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think,
unbox first, otherwise you would be comparing references.

Are there any rules? I couldn't find it in JLS.

Autoboxing:


output is :
Less than Check : true
Greater than Check : true
Equality Check : false
 
reply
    Bookmark Topic Watch Topic
  • New Topic