Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Android Security Essentials Live Lessons this week in the Android 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: 38057
    
  22
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: 14074
    
  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: 38057
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: equals() method of java.util.Arrays
 
Similar Threads
StringBuffer
dereference
Anonymous array
about == and equals()
Puzzling Transactions.....