2

This a section of my Java program I've taken out and simplified to test. The task is to compare two integers from an ArrayList and state whether they are equal.

The below code works for numbers <128 but any number >128 and the code will not work.

Any help would be really great, thanks.

import java.util.*;

public class test
{
public static void main (String[] args)
{

Integer seat1Store = 128;
Integer seat2Store = 128;
Integer seat3Store = 0;
Integer seat4Store = 0;
Integer seat5Store = 0;


ArrayList<Integer> proceedArray = new ArrayList<Integer>();


if (seat1Store !=0)
{
    proceedArray.add(seat1Store);
}
if (seat2Store !=0)
{
    proceedArray.add(seat2Store);
}
if (seat3Store !=0)
{
    proceedArray.add(seat3Store);
}
if (seat4Store !=0)
{
    proceedArray.add(seat4Store);
}
if (seat5Store !=0)
{
    proceedArray.add(seat5Store);
}

System.out.println("ArrayList = " + proceedArray);


boolean proceed = false;


for(int i = 0; i<proceedArray.size();i++)
{
    for(int p=0; p<proceedArray.size(); p++)
    {
        if(i != p)
        {
            if(proceedArray.get(i) == proceedArray.get(p))
            {
                System.out.println("DUPLICATE");
                System.exit(0);
            }
        }
    }
    proceed = true;
}


if (proceed == true)
{
    System.out.println("PROCEEDED");
}




}
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • possible duplicate of [Integer wrapper objects share the same instances only within the value 127?](http://stackoverflow.com/questions/5117132/integer-wrapper-objects-share-the-same-instances-only-within-the-value-127) – RanRag Apr 19 '12 at 07:48

3 Answers3

8

Yes, that's expected. You should not compare object references with == or !=. You should use .equals(..) instead, or better - use the primitive int rather than Integer.

The thing is, values up to 128 are cached, and the JVM gives you the same objects (hence the reference comparison works). Above 128 it creates a new instance. Look at the javadoc of Integer.valueOf(int) (which is what happens behind the scene)

Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

When comparing objects in Java, you're actually comparing the references and not the values when using the == equality operator. Instead, you should use the method .equals() to compare values;

Integer a = 2423;
Integer b = 5455;

if (a.equals(b)) { ...
Björn
  • 29,019
  • 9
  • 65
  • 81
0

You use Integer object instead of int primitive as variable types. That will only work with '=' operator for values up to 128, because java caches it. Correct way to compare object is the .equals() function.

But use primitive values instead.

int i1 = 1;
int 12 = 2;
List<Integer> values = ArrayList<Integer>();
if (!values.contains(i1)) {
 values.add(i); 
}
if (!values.contains(i2)) {
 values.add(i2); 
}
d56
  • 825
  • 1
  • 9
  • 26