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 "=="



Rick Blair wrote:

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

This is demonstrably untrue.  I just tried it (source provided below), and
it simply doesn't work that way, under Java 5 or any earlier version I can
get my hands on to run.

Well, maybe it's true if the number of instances is allowed to be 0, i.e.
there are no "cached" instances at all, but a nonexistent cache could
hardly be called a cache.


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

This is also demonstrably untrue (source not provided, but easily inferrable).

There is only one instance each for Boolean.TRUE and Boolean.FALSE, but
that's really a different question.  And yes, those have existed since Java
1.0.


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

Yes, per ClassLoader, there is only one Boolean.TRUE or Boolean.FALSE.
Everything else above that I said is demonstrably untrue, is also untrue
per ClassLoader.


Maybe you're thinking of ObjC, where an init function need not return the
same instance.  Otherwise I can assure you, any number of new Integer(n) or
new Boolean(b) invocations will return a different instance.

In a sense, the language REQUIRES this, because the creation operator MUST
create a new instance, and the <init> method (i.e. constructor) MUST
receive a "blank" object.  That's considering the operations at the JVM
byte-code level.

The trick with auto-boxing is done at the source level, i.e. the compiler
is doing it, and producing byte-codes that have a non-obvious relationship
to the source.  In particular, the compiler is calling Integer.valueOf(int)
when it auto-boxes an int value.


Or maybe you're thinking of Integer.valueOf(int) instead of new Integer(),
which is what the compiler does auto-boxing with.  That's a public static
method, and you can call it yourself.  It's not just for auto-boxing.  But
if that's what you're thinking of, then it only applies to Java 5, because
there is no Integer.valueOf(int) prior to Java 5.


Test program follows:

public class boxer {

  public static void main( String args[] )
  {
    Integer a = new Integer( 1 );
    Integer b = new Integer( 1 );
    Integer c = new Integer( 1 );
    Integer d = new Integer( 1 );

    equal( a, a );
    equal( a, b );
    equal( a, c );
    equal( a, d );
    newline();

    equal( b, b );
    equal( b, c );
    equal( b, d );
    newline();

    equal( c, c );
    equal( c, d );
    newline();

    equal( a, 1 );
    equal( b, 1 );
    equal( c, 1 );
    equal( d, 1 );

    equal( 1, 1 );
    equal( 3, 3 );
    equal( 4, 5 );
  }

  private static void equal( Integer a, Integer b )
  {
    System.out.println( " " + a + " == " + b + ": " + (a == b)
        + ", " + a + " equals " + b + ": " + a.equals(b) );
  }

  private static void newline()
  {  System.out.println();  }

}

Compile and run it yourself to see the results.

To see what auto-boxing is doing:
  javap -classpath boxer.jar -c boxer

I also recommend getting the source for java.lang.Integer and looking at it
to see what range of values it has in its "cache".

  -- GG


 _______________________________________________
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



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.