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);
2
  • 2
    Hope this will help : stackoverflow.com/questions/8392771/… Commented Oct 10, 2015 at 6: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. Commented Oct 10, 2015 at 7:06

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

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.

4 Comments

Does the '==' operator only use check for the memory location of the two objects?
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.
So if I had used the equals() here then the output should have been true?
byte is primitive type- you need to use Arrays.equals(). stackoverflow.com/questions/9499560/…
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'

1 Comment

so the clone() function does not give the same memory location but give the same data with a different memory location ?
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

Comments

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]);}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.