Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wrong behaviour when checking the equality of two elements of an ArrayList<Integer> using the operator "=="



Autoboxing is just a compiler feature. The compiler substitutes it with say Integer.valueOf(int) and if you take a look at the source of the Integer class you'll see that it's caching the Integer Objects from -128 through 127.

Anyways, all three samples are wrong. The new operator will always create a new Object and == compares only the reference.

 Roger

On Jul 9, 2007, at 9:10 PM, Rick Blair wrote:

Actually it goes much deeper than autoboxing. The Java spec states that a number of instances for primitive wrapper types must be created. For numeric types I think the values are from 0 to 256 must be included, however the max is up to the JVM provider. I think this has been true from Java 1.0

Therefore new Integer(0) == new Integer(0) ... new Integer(255) == new Integer(255)


Also there is only one new Boolean(true) and one new Boolean(false) in any given JVM.


Ok I might have lied. It may be on a ClassLoader basis instead of a JVM basis, but the jist is the same.

Cheers

Rick




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: wrong behaviour when checking the equality of two elements of an ArrayList<Integer> using the operator "==" (From: Greg Guerin <email@hidden>)
 >Re: wrong behaviour when checking the equality of two elements of an ArrayList<Integer> using the operator "==" (From: Rick Blair <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.