• 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

'==' operator and autoboxing

 
Ranch Hand
Posts: 71
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii everyone, i have small doubt in how autoboxing works with regards to the == operator.

in the code below:



can anyone please explain that what happens when true is returned in ##1 and ##2.
is it that in both ##1 and ##2 the Integer is unboxed to an int? if so then is it a rule tht when comparing an int & Int, Int is always unboxed to int?

since this rule does not seem to hold in switches. In switches, int is Autoboxed to Int when case constants are matched to the switch expression as shown: when we use an Integer value in switch expression and a int value in the case expression, then while matching the case constants to the expression value, int is converted to an Integer. for example:



it would be great if someone can explain. thanks
 
Greenhorn
Posts: 26
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use == operator primitive type with its corresponding Wrapper Class object,
then the Corresponding Wrapper class object unboxing to corresponding primitive type
and then comparison stats, So the out put like this(For line no 6 and 7).

When you create Wrapper class() object without using new operator,
(for ex -Integer d=100;) it checks whether the corresponding value (after = operator) lies between -128 to 127 or not.
if yes, it creates object with that value and put it its Constant-poll .
When you create a another object with the same value ,it checks whether it available in its constant-poll or not.
if yes, it does not create a second object just send the reference of 1st one.

So the out put is false ( because the value is 300 ,i.e out of range).

ALL THE ABOVE THING ONLY FOR 4 Wrapper class (Integer,Byte,Short,Character).
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks buddy. i have some further doubt:

Bhagirathi Mallick wrote:When you use == operator primitive type with its corresponding Wrapper Class object,
then the Corresponding Wrapper class object unboxing to corresponding primitive type
and then comparison stats, So the out put like this(For line no 6 and 7).



in case of a switch statement, how is the case constant compared to the swtich expression? i guess it uses == for comparing and matching. if i am correct so far then as shown in my second example regarding switch in my primary post, int is being compared to Integer and instead of unboxing Int to int, int is being autoboxed to Int. is it an exception to the rule? or please correct me.

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


Long as well
 
Bhagirathi Mallick
Greenhorn
Posts: 26
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First Thing, you can use a variable(Only primitive) in case: if and only if that
variable is final and initialize at the time of declaration. but you use a non final byte variable.
Second thing all primitive type can auto boxed their corresponding Wrapper type.
so byte can not auto boxed to Integer

Third Case whenever you use wrapper type in switch , then the corresponding wrapper type
auto boxed to primitive type and case constant should be
corresponding primitive value or corresponding primitive final variable.

if you use final int b=4; it compiles successfully.not final Integer b=11;
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Bhagirathi, got it
reply
    Bookmark Topic Watch Topic
  • New Topic