• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between array1.equals(array2) and Arrays.equals(array1 , array2)  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Started recently the OOP part of Java but i would have expected array1.equals(array2) and Arrays.equals(array1 , array2) to output the same thing as arrray is an object . Why array1.equals(array2) is false when we have two arrays with the same values in the same places and what is the difference between array1.equals(array2) and Arrays.equals(array1 , array2) ?
 
Anuj Sharma R
Greenhorn
Posts: 28
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are doing array1.equals(array2), it does not take into consideration the elements of the array.

For Arrays.equals - it takes into consideration the order and elements of the array into consideration.

Below links should give you a good understanding of the both:
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals%28java.lang.Object%29
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#equals%28boolean[],%20boolean[]%29
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals method in the arraay is unoverridden, so it does the same as i Object (as in the link you were given). It returns whether the Object passed is the same object.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The equals method in the arraay is unoverridden, so it does the same as i Object (as in the link you were given). It returns whether the Object passed is the same object.

It is, however, overloaded, and I think this is what Tiberius is referring to:

@Tiberius: Campbell is right. The equals() variants you see in the Arrays class allow you to do a "shallow" comparison of two arrays. The class does not override Object's equals() method though.

However, an array of Objects could include arrays as elements, which is why the deepEquals() method is also provided. This performs a recursive equality check on any elements that are themselves arrays.

HIH

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:The equals method in the arraay is unoverridden, so it does the same as i Object (as in the link you were given). It returns whether the Object passed is the same object.

It is, however, overloaded

I think Campbell is referring to an array, not the Arrays class. An array neither overrides nor overloads the equals method.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the input
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I was. Thank you, Joanne, for the clarification.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!