Integer Class Tutorials

public boolean equals(Object obj)

The equals() method compares the value of the parameter to the value of the current Integer object, if the value is the same, the method will return true, otherwise it will return false. For example,
Integer ref1 = new Integer( 419 );
Integer ref2 = new Integer("419");
Integer ref3 = 419;

ref1.equals( ref2 ); // true
ref1.equals( ref3 ); // true
ref1.equals( 419 ); // true
ref1.equals( 612 ); // false

Integer cache

It is very easy to confuse the functionality of the == equality operator and the .equals() method; the == equality operator has some super strange behavior relating to the Integer class. Recall that a reference variable holds a reference to an object located on the heap memory. Normally the == equality operator will determine if a reference variable refers to the same object on the heap.
Integer ref1 = new Integer( 419 );
Integer ref2 = new Integer( 419);
Integer ref3 = ref1;
ref1 == ref2;
// false, they each point to separate instances (objects) on the heap.
ref1 == ref3;
// true, they point to the same instance (object) on the heap.

There is a special feature called the Integer cache that exists to save memory and improve performance. The Integer cache works like this, first it checks for an Integer instance with same value in the cache, if one does exist it just returns that reference, otherwise a new Integer instance will be created in the cache and that reference is returned.
Here's the first catch on the cache ... by default, it only applies to values from -128 to 127.

Integer i1 = 127;
Integer i2 = 127;
i1 == i2;
// true, they point to the same instance (object) on the heap.
Integer i3 = 128;
Integer i4 = 128;
i3 == i4;
// false, they each point to separate instances (objects) on the heap.

Here's the second catch on the cache. Integer caching only applies to autoboxing – Integer objects will not be cached when created by using a constructor.
Integer i5 = new Integer( 127 );
Integer i6 = new Integer( 127 );
i5 == i6;
// false, they each point to separate instances (objects) on the heap.
Integer i7 = new Integer( 128 );
Integer i8 = new Integer( 128 );
i7 == i8;
// false, they each point to separate instances (objects) on the heap.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md IntegerEquals
C:\Java>cd IntegerEquals
C:\Java\IntegerEquals>Notepad IntegerEquals.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class IntegerEquals {
    public static void main(String args[]) {
        Integer ref1 = new Integer(419);
        Integer ref2 = new Integer("419");
        Integer ref3 = 419;        
        System.out.println("ref1.equals(ref2) = " + ref1.equals(ref2));
        System.out.println("ref1.equals(ref3) = " +ref1.equals(ref3 )); 
        System.out.println("ref1.equals(419) = " +ref1.equals(419));
        System.out.println("ref1.equals(612) = " +ref1.equals(612));

        System.out.println("");

        Integer r1 = new Integer(419);
        Integer r2 = new Integer(419);
        Integer r3 = r1;
        System.out.println("r1 == r2 " + (r1 == r2)); 
        System.out.println("r1 == r3 " + (r1 == r3)); 

        System.out.println("");
        
        Integer i1 = 127;
        Integer i2 = 127;
        System.out.println("i1 == i2 " + (i1 == i2)); 
        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println("i3 == i4 " + (i3 == i4)); 


        Integer i5 = new Integer(127);
        Integer i6 = new Integer(127);
        System.out.println("i5 == i6 " + (i5 == i6)); 
        Integer i7 = new Integer(128);
        Integer i8 = new Integer(128);
        System.out.println("i7 == i8 " + (i7 == i8)); 

        System.out.println();        
        System.out.println("i1 == 127 " + (i1 == 127)); 
        System.out.println("i3 == 128 " + (i3 == 128)); 
        Integer tricky = 8675309;
        System.out.println("tricky == 8675309 " + (tricky == 8675309)); // auto-unbox
    }
}

Now switch back to the command prompt (CMD) and type in javac IntegerEquals.java and press Enter.
Now type in java IntegerEquals and press Enter.


C:\Java\IntegerEquals>javac IntegerEquals.java
C:\Java\IntegerEquals>java IntegerEquals
C:\Java\IntegerEquals>java -XX:AutoBoxCacheMax=256 IntegerEquals
ref1.equals(ref2) = true
ref1.equals(ref3) = true
ref1.equals(419) = true
ref1.equals(612) = false

r1 == r2 false
r1 == r3 true

i1 == i2 true
i3 == i4 false
i5 == i6 false
i7 == i8 false

i1 == 127 true
i1 == 128 true
tricky == 8675309


Final thoughts

The equals() method is fairly simple to understand. However, the == equality operator is quite complicated and I highly recommend against using it any circumstances.


Tutorials