aspose file tools*
The moose likes Java in General and the fly likes equals() for arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals() for arrays" Watch "equals() for arrays" New topic
Author

equals() for arrays

Serge Adzinets
Ranch Hand

Joined: Nov 26, 2002
Posts: 166
Hi all,

I've recently found that equals() is not overriden for the array type, it's inherited from Object. Is there a good reason for this? Why not to override it doing the same as Arrays.equals()?

I use JUnit's assertEquals() method, and it's not overloaded for array types, so assertEquals(Object, Object) is used, and of course it doesn't work properly.

Serge
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I think this falls under the "Yes, that'd be cute" category. If the Java folks had thought of this way back when, they might indeed have implemented it, and it might have been useful. But introducing this now would likely break a lot of code, so don't look for it any time soon.


[Jess in Action][AskingGoodQuestions]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

Serge,
A common technique for dealing with this in JUnit is to write your own custom assertions and put them in a test superclass that extends test case. Your tests then extend that new superclass.

This way, you can write your own assertEquals(Object[] array1, Object[] array2) and have it do what you want.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Serge Adzinets
Ranch Hand

Joined: Nov 26, 2002
Posts: 166
Yeah Jeanne, this could be a workaround, but doesn't work if you have classes that extend Assert, not my base test case. I have a helper class like this. Anyway, it's not a big problem to change it.
[ April 21, 2005: Message edited by: Serge Adzinets ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: I think this falls under the "Yes, that'd be cute" category.

I'd put it in the "Yes, Java's designers should be kicking themselves for not including better methods for arrays from the beginning" category. But either way, I agree it's not likely to happen now; the window of opportunity was missed.

[Serge]: Yeah Jeanne, this could be a workaround, but doesn't work if you have classes that extend Assert, not my base test case.

Sure it works - with minor modifications to what Jeanne said. Make your new method static, just like all the other asserts in JUnit's Assert class. Then you can call it quite easily with something like "MyAssertionClass.assertArraysEqual(arr1, arr2);". If you don't like typing the "MyAssertionClass", well JDK 1.5 allows you static imports. But that's a minor issue. It's quite easy to call public static methods from anywhere you want; it just takes a tiny bit more typing.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() for arrays