*
The moose likes Beginning Java and the fly likes equals() method of java.util.Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() method of java.util.Arrays" Watch "equals() method of java.util.Arrays" New topic
Author

equals() method of java.util.Arrays

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178


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

Joined: Oct 13, 2005
Posts: 38511
    
  23
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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear Jesper & Campbell,
Thanks for the reply


Regards,
Vijay
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equals() method of java.util.Arrays