File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Arrays.equals and my overridden equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays.equals and my overridden equals" Watch "Arrays.equals and my overridden equals" New topic
Author

Arrays.equals and my overridden equals

Tom Cwir
Greenhorn

Joined: Mar 22, 2013
Posts: 2


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

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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") ?


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Tom Cwir
Greenhorn

Joined: Mar 22, 2013
Posts: 2
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

Joined: Jul 27, 2010
Posts: 1157

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays.equals and my overridden equals
 
Similar Threads
equals() , instanceof
hashCode contract
Doubt in equals()
equlas()
equals() contract problem