Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() method of java.util.Arrays

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Dear friends,
The above statement gives compiler error. In the API there is one overloaded equals() method which takes two Object arrays as parameters.

Kindly explain the concept hidden in the above statement.

Regards,
Vijay
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API specification for Arrays? It is overloaded as equals(int[], int[]) or equals(Object[], Object[]), but not as equals(int[], Object[]).

Unboxing will not convert int[] to Integer[] or vice versa. You will have to extend Arrays (it's not final) and implement an overridden equals(int[], Integer[]) method.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
... You will have to extend Arrays (it's not final) and implement an overridden equals(int[], Integer[]) method.

Class java.util.Arrays is indeed not final, but it has a private constructor, so you cannot extend it anyway. It's not meant to be instantiated or extended; it's just a bunch of static utility methods.
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jesper & Campbell,
Thanks for the reply


Regards,
Vijay
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I hadn't noticed the private constructor. You are right; it won't work with a private constructor. You would have to create your own MyArrays class with an equals(int[], Integer[]) method.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic