Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Wrappers and primitives comparison:

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone , extracted this from the API topic from the Tiger's Handbook.

Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE

Do not understand why the results for the 3 comparisons are different since all of them are performing auto-boxing?

Thanks in advance.

Cheers.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
huh. weird one. messed with it...



and got...



would like to know more about this. looks like a size constraint at 2^7. feedback anyone?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there an online source for this? have been googling it some, not finding good stuff yet.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting... weird ...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bit more to this story. ian posted interesting comment on blog. took this insight and expanded orig snippet:



and got...



funky.
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like valid values for byte are the only ones this trick works for.

However, trusting on autoboxing for comparing primitive wrappers with = is something you should avoid, since there is ambiguity: which comparison should be used, the autoboxed comparison on the primitive value or the comparison on reference value?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we talked about it here and feel for now that this explains it...


The Integer class keeps a cache of Integer instances representing values from -128 to 127. When references are made with values within this range, the corresponding cached instance is returned, so, for values within this range, == is returning true because the operands are in fact the same instance (from the cache). This is not the case with values outside the cached range. In those cases, the operands are different memory locations, and == evaluates to false.



seems like care needs to be exercised with autoboxing. the possibility of being surprised by this behavior, and the ease with which one may incur an NPE are things to keep in mind.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rob, our conversation about this thus far has us leaning towards use of equals() for wrapper, and/or valueOf(). methinks you are rightin advising others to avoid ==.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like autoboxing uses Integer.valueOf to create the wrapper. This method uses a cache of pre-allocated Integer objects for the range -128 to 127. So the same instance is returned on multiple calls to create a wrapper for the same number. Thus the == operator return true since it is the same instance. Here is the code in java.lang.Integer for valueOf:
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread...

https://coderanch.com/t/251353/java-programmer-SCJP/certification/Boxing-Unboxing
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Mr foo"

Please click on the My Profile link above and change your display name to match JavaRanch's Naming Policy of using your real first and real last names.

Thanks

Mark
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
First of all this code will not compile.

Integer j1 = 2;
Integer j2 = 2;

Can you plz expline why & how you are compiling this code.??
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ..purujit..

This is possible in jdk1.5

not in earlier versions...

a feature called autoboxing

Regards

[ November 08, 2005: Message edited by: A Kumar ]
[ November 08, 2005: Message edited by: A Kumar ]
 
Rob Spoor
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing is implicit conversion between numeric (also character/boolean? I'm not that familiar with 1.5) primitives and their wrapper classes.

Where in 1.4 and before you had to write Integer i = new Integer(2) or int i = new Integer(2).intValue(), 1.5 does that for you when you write Integer i = 2 or int i = new Integer(2).
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Autoboxing is



and some argue that it can be a real stinker.

i've happily used it for stuff like this:

 
Rob Spoor
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's basically what it was meant for I think - for storing primitives in collections without the extra effort for explicit transforming between the two.
Which is a real pain, I know...


More generally: autoboxing is meant for code where you need to pass a primitive but an object is expected.
[ November 09, 2005: Message edited by: Rob Spoor ]
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc, your last name can't be "Foo".

Mark
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marc, your last name can't be "Foo".

Why couldn't it? My local telephone directory has 30 people whose last name is "Foo".
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

whose last name is "Foo"



calling to mind some funny dialog from a great (imo) movie.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a real pain



ee-yup

comes out of the ResultSet as ints, need to get into the bean as Integers.
[ November 10, 2005: Message edited by: Erick Reid ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
first fall the code own't compile, as some one said ... this might not be true with 1.5. But why the output would be true.. in this case

Integer i = 2;
Integer j = 3;

Wrapper classes are used to get primitives the advantages of objects...
so, then you need to use .equals method to compare the objects right... then only i.equals(j)) would be true . I would be thankful if someone could explain me how i==j would be true......
thanks in advance
sruthi
 
sruthi gundu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
first fall the code own't compile, as some one said ... this might not be true with 1.5. But why the output would be true.. in this case

Integer i = 2;
Integer j = 2; ( 2 instead of 3, in the previous forum i posted it as 3 iam sorry)

Wrapper classes are used to get primitives the advantages of objects...
so, then you need to use .equals method to compare the objects right... then only i.equals(j)) would be true . I would be thankful if someone could explain me how i==j would be true......
thanks in advance
sruthi
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys -

You've the Integer part of this thing worked out, if you check out section 5.1.7 of the new language spec. you'll find the rest!

hth,

Bert
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
language spec? we don't need no stinking language spec!
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add a point to Ray Horn's views


As suggested by Bert, a piece of info from language spec,

If the value being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p.
It is always the case that r1 == r2.

I assume that explains the behaviour..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic