1

The output of the following codeblock is 'false'. Why?

byte[] a = {1,2,3};
byte[] b = (byte[]) a.clone();
System.out.print(a==b);
Gautam
  • 1,145
  • 1
  • 9
  • 10
  • 2
    Hope this will help : http://stackoverflow.com/questions/8392771/how-does-an-arrays-equal-method-work – Rahman Oct 10 '15 at 06:31
  • I don't think this is a duplicate. The other question asks "why `a.equals(b)` is false", this one asks "why `a == b` is false". The answers on the other question talk about `equals`, and don't touch on *identity* (naturally, why would they?), but that is the key subject here. The distinction of these topics is crucial and fundamental when working in Java. – janos Oct 10 '15 at 07:06

6 Answers6

4

array does not override equals method . So it will go for reference equality check which is false in this case

Rahman
  • 3,755
  • 3
  • 26
  • 43
4

== is the identity operator in Java. It means, it checks if two objects are the same thing. It's not about logical equality, it's not about containing the same values. It's about being the same object.

In your example, they are not the same object. The clone method of an array creates a new object, with the same content as the original. The new object will have the same content, but it will have its own identity. It's a new object, distinct from the original.

In contrast, the equals method is about logical equality. That is, two objects containing equal values. When cloning an array, the original and the clone are equal, because they have the same values. But they are not identical, because they have different identities. In terms of Java's == operator and the contract of equals method:

  • If two arrays are equal, they are not necessarily identical
  • If two arrays are identical, they are necessarily equal

The importance of identity is that when two array variables have the same identity (both pointing to the same object), then modifying one of them will appear to modify both of them. When two arrays are equal but not identical, you can modify one without affecting the other: they are completely independent objects.

janos
  • 120,954
  • 29
  • 226
  • 236
3

Because a and b are not referencing to the same object. When you use clone() function, a new reference object is created in java.

tanjir
  • 1,294
  • 13
  • 22
  • Does the '==' operator only use check for the memory location of the two objects? – Gautam Oct 10 '15 at 06:41
  • In java there is no concept of pointer.. but reference address of an object in JVM. == looks for if they are "exactly" same - that means the same object (reference address are same). One thing to note here is the reason why we use equal() function is because you can compare the contents of the objects and return true or false regardless of the location. – tanjir Oct 10 '15 at 06:44
  • So if I had used the equals() here then the output should have been true? – Gautam Oct 10 '15 at 06:46
  • byte is primitive type- you need to use Arrays.equals(). http://stackoverflow.com/questions/9499560/how-to-compare-the-java-byte-array – tanjir Oct 10 '15 at 06:49
1

In your out put you are comparing two instances of different objects which are not the same. Think of this as the location in memory where the object is stored. So the false printout is not due to the clone, but the comparison.

Cloaning means you create a copy of the object that has the same values. What exactly that means is not defined: a clone is usually not a deep clone so cloning an object A -> B referencing Object c, then usually c will be referenced from both A and B.

A good reference is the Javadoc and Joshua Bloch's Effective Java Item 11:'Override clone judiciously'

hotzst
  • 7,238
  • 9
  • 41
  • 64
  • so the clone() function does not give the same memory location but give the same data with a different memory location ? – Gautam Oct 10 '15 at 06:39
0
byte[] a = {1,2,3};
byte[] b = a.clone();
boolean isEqual = Arrays.equals(a, b);
System.out.print(isEqual);

For details about clone : http://www.javatpoint.com/object-cloning

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
0

Think you would might be trying to compare the elms inside the arrays.

for(int i = 0; i<a.length; i++)
{ System.out.println(a[i] == b[i]);}
Aven
  • 72
  • 10