8

I'm learning Java and just came up with this subtle fact about the language: if I declare two integer Arrays with the same elements and compare them using == the result is false. Why does this happen? Should not the comparison evaluate to true?

public class Why {

    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};

        System.out.println(a == b);
    }

}

Thanks in advance!

4
  • 22
    No, because == compares references, not values. Commented Jan 30, 2013 at 22:18
  • 3
    Others have answered the question, but I'll add that this is a very important thing to understand in Java, or any language that has objects or structs as references. As written, you are creating two separate objects that happen to have the same numbers in them. If you had written "int[] b = a;" then b would be pointing to the same memory as a, and then a==b would be true. Commented Jan 30, 2013 at 22:26
  • @jfrank That's a bit too generic. C# and python for example do have objects as references but == will still give the right result (well for python at least, c# has its own downfalls there). Commented Jan 30, 2013 at 22:55
  • I didn't mean to imply that == behaved the same in each language with references, so my bad if that's how it came out. I meant to emphasize the importance of understanding the notions of references and memory, in order to program successfully in those languages. IMHO there is no "right" behavior for the == operator, since equality is a nuanced concept when you have references. If you don't understand references, you have no chance to understand the different kinds of equality properly. Commented Jan 30, 2013 at 23:16

3 Answers 3

35

Use Arrays.equals(arr1, arr2) method.

The == operator just checks if two references point to the same object.

Test:

int[] a = {1, 2, 3};
int[] b = a;    
System.out.println(a == b);   // returns true as b and a refer to the same array  

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b));   //returns true as a and b are meaningfully equal
Sign up to request clarification or add additional context in comments.

1 Comment

Howto find difrerences without using a loop?
1

No. ==compares numerical (or boolean) values, or references, only.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21

You're probably looking for the Arrays.equals (a,b) method

Comments

0

If you use == operator with Object you are comparing the references, not the values.
If you use == operator with primitive types (int, long, boolean...) you are checking if they have the same values.

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};

System.out.println(a == b); //return false;

System.out.println(a[0] == b[0]); //return true;



String[] a1 = {"Cat", "Dog", "Mouse"};
String[] b2 = {"Cat", "Dog", "Mouse"};

System.out.println(a1 == b1); //return false;

System.out.println(a1[0] == b1[0]); //return false; Because String are Object

You can use the Arrays.equals(array1, array2) method.

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.