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 Using the equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using the equals method" Watch "Using the equals method" New topic
Author

Using the equals method

Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
Hi, I am attempting to check if two die are equal to eachother and im having a bit a trouble.

So far i have been given code in the DiceSimulator class that creates an object for each die



Now i have been having trouble with an equals method in the Die class. So far I have this and i am getting the error that die1 and die2 cannot be resolved. Any ideas? I have a feeling this is a quick fix.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The error states that the variables die1 and die2 cannot be resolved. Do you have them declared some where?

Do you really need them? Do you want to compare two objects of the Die class? Try like below



If you want to compare some variables of the two Die objects, you got to change the condition being checked in the if.
Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
John Jai wrote:The error states that the variables die1 and die2 cannot be resolved. Do you have them declared some where?

Do you really need them? Do you want to compare two objects of the Die class? Try like below



If you want to compare some variables of the two Die objects, you got to change the condition being checked in the if.


say i wanted to compare a variable of the die object known as "spots" where in the equals code would i reference this?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Might be like below if its a String... Shall use == if it's a primitive..

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

Harsha's code also highlights another flaw in your original code. The equals method requires java.lang.Object as its parameter type. If it's something else you're not overriding the equals method but overloading it instead.
And if you're overriding equals you should also override hashCode.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
There is a redundant bit of code in that method, and a potential error which may cause the equals method not to fulfil its general contract.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Campbell Ritchie wrote:There is a redundant bit of code in that method, and a potential error which may cause the equals method not to fulfil its general contract.

If you mean Harsha's, I spotted the redundancy, but not a potential error. Could you elaborate?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Winston Gutkowski wrote:I spotted the redundancy
Could you please tell where it is...
are those these?

and
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

Winston Gutkowski wrote:If you mean Harsha's, I spotted the redundancy, but not a potential error. Could you elaborate?

I think he means the use of instanceof. Using that in an equals() method can lead to problems unless the class or the method is final. Think about how you might implement equals() in a subclass.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

And the redundancy is in o!=null && o instanceof Die. If o instanceof Die then o != null.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Matthew Brown wrote:I think he means the use of instanceof. Using that in an equals() method can lead to problems unless the class or the method is final. Think about how you might implement equals() in a subclass.

Oh, OK. I don't really think of that as a potential error, because using getClass() can cause other problems, such as violating LSP; and you have to use one or the other (unless you want to get quite involved). Personally, I always use instanceof unless I've got a good reason not to.

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

True, but it can lead to you breaking commutativity, which breaks the contract.

The way I see it, if you can make the equals() method final there's no problem. If you can't then there's no perfect solution, and it's a matter of what you can live with.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
If you make the class final, then there is no such problem either.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Campbell Ritchie wrote:If you make the class final, then there is no such problem either.

Yeah. That one actually solves a lot of problems.

In fact, I don't know why more schools don't recommend getting into the habit of doing it as a matter of course (like making variables private). After all, you can always remove it later on if it's too restrictive.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
I suspect that a lot of teachers don’t know about final.
 
 
subject: Using the equals method