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?
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
Joined: Nov 22, 2008
Posts: 18944
posted
0
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 ==.
Ray Horn
Ranch Hand
Joined: Oct 20, 2005
Posts: 39
posted
0
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:
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.
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).
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 ]
comes out of the ResultSet as ints, need to get into the bean as Integers. [ November 10, 2005: Message edited by: Erick Reid ]
sruthi gundu
Greenhorn
Joined: Aug 11, 2005
Posts: 11
posted
0
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
Joined: Aug 11, 2005
Posts: 11
posted
0
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
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
posted
0
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
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
language spec? we don't need no stinking language spec!
Cheenu Subramanian
Ranch Hand
Joined: Aug 15, 2005
Posts: 40
posted
0
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..
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.