3

Why does == not work with byte arrays in Java?

For example:

    byte[] a = new byte[]{1,2,3,4};
    byte[] b = new byte[]{1,2,3,4};

    a == b //false
    a.equals(b) //false
    Arrays.equals(a,b) //true
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 4
    because `==` compares references and not content(for objects atleast, and arrays are treated as one). – SomeJavaGuy Mar 29 '16 at 07:45
  • See the following [What is the difference between == vs equals() in Java?](http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) –  Mar 29 '16 at 07:48
  • 4
    Also see here: [equals vs Arrays.equals in Java](http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java) - The default implementation of `equals()`, which arrays don't overwrite, uses `==` as well. – Thomas Mar 29 '16 at 07:49
  • @KevinEsche It doesn't answer the `a.equals(b) // false` though. When comparing arrays Java language does something counter-intuitive compared to a regular object. `.equals` does a `==` behind the scenes. [Here is some more information with a similar question regarding primitive-arrays.](http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java) – Kevin Cruijssen Mar 29 '16 at 07:49
  • @KevinCruijssen The original Title of the question didn´t ask for that. – SomeJavaGuy Mar 29 '16 at 07:51
  • @KevinEsche I know, the question itself did though. A typical example of just reading the title and commenting, instead of reading the actual question itself. (Hence my edit to the title. Mainly OP's fault, but I suggest reading an entire question before commenting purely based on the title.) – Kevin Cruijssen Mar 29 '16 at 07:52

5 Answers5

5

== and byte[] realization of equals uses links comparison. In this case links point to different regions in memory. If you follow to source code for equals realization for byte[], you'll see the following:

public boolean equals(Object obj) {
    return (this == obj);
    }

This actually the default implementation from Object

Arrays.equals(a,b) uses comparison of the contents of arrays.

Also, see the following What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
  • "If you follow to source code for equals realization for byte, you'll see..." nope. At the very least, you mean `byte[]`. – Andy Turner Mar 29 '16 at 07:56
2

In java, array is treated like an object. So == will compare only the references. Also a.equals(b) is same as a==b as default implementation in Object class check the reference only.

If you really wish to compare the content, you should use Arrays.equals(a,b). Though this will not work in multidimensional array.

If you do something like below, it will result true, as both will refer the same reference.

b = a;
System.out.println(a==b);
Kartic
  • 2,935
  • 5
  • 22
  • 43
1

As simple as equals compare if they share same memory region, and Arrays.equals compares the content of array.

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
0

variable a and b are in different memory locations. if you print the a and b it will print with different hash codes.

hence a == b and a.equals(b) gives false

In the java source code equals method code is as the following

public boolean equals(Object obj) {
   return (this == obj);
}

So equals is just like the == that you are testing. If you want to get a meaning full answer using equals method you have to override it by yourself.

and the Arrays.equals method is as the following.

public static boolean equals(byte[] a, byte[] a2) {
    if (a==a2)
        return true;
    if (a==null || a2==null)
        return false;

    int length = a.length;
    if (a2.length != length)
        return false;

    for (int i=0; i<length; i++)
        if (a[i] != a2[i])
            return false;

    return true;
}
PHCJS
  • 445
  • 4
  • 19
0

a.equals(b) just use Object.equals()(byte[] is a Object and donot override the method equals()), see the source:

public boolean equals(Object obj) {
    return (this == obj);
}

== compares reference's address.(not consider special type: String,int etc.)

Arrays.equals() compares reference's address and content, see the source:

public static boolean equals(byte[] a, byte[] a2) {
    if (a==a2)
        return true;
    if (a==null || a2==null)
        return false;

    int length = a.length;
    if (a2.length != length)
        return false;

    for (int i=0; i<length; i++)
        if (a[i] != a2[i])
            return false;

    return true;
}
magooup
  • 134
  • 6