JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (oldest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
Re: Error in boxing description (response to post by wrick) | Reply
Why between -128 and 127 only? Why not between -2^31 and 2^31-1?
Because caching 256 objectss is not expensive, while, caching over 4 billions is.

Is there any value for which this code returns false?
Obviously the equals must always work, what the OP meant was:
System.out.println(Integer.valueOf(26)==Integer.valueOf(26));
System.out.println(Integer.valueOf(260)==Integer.valueOf(260));
Re: Error in boxing description (response to post by pgc) | Reply
Why between -128 and 127 only? Why not between -2^31 and 2^31-1? Is there any value for which this code returns false?
int x = /*some value*/ ;
Integer v1 = x;
Integer v2 = new Integer(x);
Integer v3 = Integer.valueOf(x);
return v1.equals(v2) && v2.equals(v3) && v3.equals(v1);
Error in boxing description | Reply
Hello,

Thanks for the nice overview of 1.5. There is an error in the boxing description.

The tutorial says:

For example, in Java 1.5 this is perfectly valid:

Integer value = 26;

And has the same effect as writing:

Integer value = new Integer (26);

This is not correct as the language spec says this boxing operation must return Integers which are equal to other auto boxed Integers with the same value for values in the range -128 to 127. (see section 5.1.7 Boxing Conversion in "The Java Language Specification, Third Edition")

The equivalent code is really:

Integer value = Integer.valueOf (26);

The Integer.valueOf(int) static method is free to return a reference to an existing Integer that has the same value. This method enforces the language requirement for values between -128 and 127.
RSS