• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() for arrays

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic