About override the equals method can not be used when the problem of Object

Category: Java EE
2011-12-29 02:38:35

Sponsored Links
Hello, I am in the writing process when it came to the following questions, I hope prawn exhibitions:
define a class User, the User class which has a League of the set, so the equals method for re- as follows:

public boolean equals (Object obj) {

if (this == obj) {/ / an error: Incompatible operand types User and Object
return true;
if (user! = null && obj.getClass () ; == User.class) {
User user = (User) obj ;/ / prompt an error: Cannot cast from Object to User
return this. getUserStuID (). equals (user.getUserStuID ());
return false;

I was using the IDE is MyEclipse 8.0, was run over in MyEclipse 6.5 also suggested that the same mistakes.

Sponsored Links

2011-12-29 02:54:07
I tried, easy to use, is that you have an IDE problem.
Also, this sentence is wrong
if (user! = null && obj.getClass () == User.class) {
should be
if (obj! = null && obj.getClass () == User.class) {
2011-12-29 03:07:20
also Yes, I use MyEclipse 8.0 tried it, easy to use.
2011-12-29 03:21:57
override the equals method, you must override the corresponding hashCode method.
2011-12-29 03:25:03

people are comparing the userStuID User class are equal, the comparison itself is of type String. . .
2011-12-29 03:42:18

Thank you all the way north of the answer, the IDE is indeed a problem, the project delete re-build after a right.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.