Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.equals and my overridden equals

 
Tom Cwir
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code above outputs:


I've also tried deepEquals (just in case but it also outputs: "false").
I assume that it uses Object's equals() instead of myClass's equals(). Anyone knows why and how to handle this?


Thanks
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom

Welcome to the ranch. There is a flaw in your code. Specifically the equals() method. You think that you have overridden it correctly. In fact, you haven't. Whenever you define equality for your custom class, you need to override the equals() method defined in the Object class. Study the method of the Object class carefully. Then look carefully at what you have written. If you are still unable to figure out the issue, revert back.

P.S -> How do I pronounce your last name. Cwir?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

One thing immediately looks strange in your code. Why does the getX method in class myClass take a myClass object as its argument? It shouldn't take any arguments, just return x of the current object:

You get false as the second line of the output because you've not overridden the equals method of class Object correctly. The equals method is supposed to take one parameter of type Object. Your version takes a myClass instead.

So, your equals method is not overriding the equals method defined in class Object, and hence it's not called when you use Arrays.equals.

Your class naming is unconventional. In Java, class names normally start with a capital letter, for example: ArraysEquals, MyClass, instead of arraysEquals, myClass.
 
Rajdeep Biswas
Ranch Hand
Posts: 207
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:P.S -> How do I pronounce your last name. Cwir?

How do you pronounce an INDIAN word "nga" (not wrongly as "naga") ?
 
Tom Cwir
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I got it. To overwrite the equals properly I must do public boolean equals(Object o) so the MyClass should be eg.


Many thanks for your help and warm welcome!

Mansukhdeep Thind wrote:P.S -> How do I pronounce your last name. Cwir?

Something like chvir
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Cwir wrote:Many thanks for your help and warm welcome!


You are always welcome.

Tom Cwir wrote:Something like chvir


First time I have heard this name. That is why I asked. Dutch or Polish maybe? Or from some Scandinavian country?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic