This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() for arrays

 
Serge Adzinets
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Serge Adzinets
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic