5

When I am trying to compare two int array, even though they are exactly the same, the code inside if (one == two) still doesn't get executed. Why is this?

Object[] one = {1,2,3,4,5,6,7,8,9};
Object[] two = {1,2,3,4,5,6,7,8,9};

if (one == two){
    System.out.println("equal");
} else {
    System.out.println("not equal");
}
4
  • 4
    you can use Arrays.equals(one,two). Please remember that, here order matters. Commented Apr 3, 2016 at 9:46
  • Thank you for the suggestion. may i ask why I can't compare two int array using ==? Commented Apr 3, 2016 at 9:47
  • 2
    When you compare two int arrays using == , you are actually comparing array references not the content of the array. Commented Apr 3, 2016 at 9:47
  • 1
    @TonyStark because if you do one == two you are comparing the references and not the content... Commented Apr 3, 2016 at 9:51

6 Answers 6

6

A few things to note here:

  • == compares the references, not the values . . . that is, you are asking whether these two arrays are the same exact instance, not whether they contain the same values.
  • The fact that you are using == means you may not know about the equals() method on Object. This is not the method you'll need to solve this current problem, but just be aware that in general, when you compare the values of two objects, you should be using obj1.equals(obj2), not obj1 == obj2. Now == does work with primitives like int (e.g. plain old x == 3 and so on), so maybe that's why you were using it, but I just wanted to make sure you were aware of equals() vs. ==.
  • In the old old days (pre-1998), you would have to compare each element pair of the two arrays. Nowadays, you can just use that static Arrays.equals() method on the java.util.Arrays class. This method is overloaded for all the primitive types (using == under the hood for each element pair) and for Object (where it will most definitely use equals() for each pair.)
Sign up to request clarification or add additional context in comments.

1 Comment

This is a decent answer but the question is poorly researched and has been asked before. For everyone's sake: look for a duplicate before answering -- it will save both you and the asker time and avoids duplicating efforts.
2

The == operator does a reference equality check on objects (which arrays are). If the array elements are primitive (like int), you can use java.util.Arrays.equals. If they are themselves Objects, java.util.Arrays.deepEquals will do a deep equality test (provided the Objects in the array supply a suitable override of Object#equals.

Comments

2

You can do something like that:

public boolean compareArrays(int[] a, int[] b) {
    boolean check = true;
    if (a!= null && b!= null){
      if (a.length != b.length){
          check= false;
      }else
          for (int i = 0; i < b.length; i++) {
              if (b[i] != a[i]) {
                  check= false;    
              }                 
        }
    }else{
      check= false;
    }
    return check;
}

Or you could do something like that:

boolean areEqual = Arrays.equals(Arrays.sort(arr1), Arrays.sort(arr2));

Comments

1

You are comparing references. Compare the content instead with: Arrays.equals(one, two)

Comments

1

If you do this:

if (one == two){

then yo are comparing the references of the 2 arrays and not their content and that is wrong..

do instead

Arrays.equals(one, two)

Comments

1

== operator compares references and not actual values of everything that extends Object in java. This approach works on primitives only.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.