equals() and hashcode(), У них много разных мест. equals(), если мы не переопределяем его из Object, это означает, что две переменные указывают на одну и ту же кучу объекта?
public Class Student(){
private int id;
private name;
public Student(int id,String name){
this.name=name;
this.id=id;
}
public void main(String[] args){
Student A=new Student(20,'Lily');
Student B=new Student(20,'Lily');
boolean flag=A.equals(B)//flag=flase;
/*
*Although they attribute the same, but they are two different objects, they point to different memory
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Student s=(Student)obj;
return new Integer(this.id).equals(new Integer(s.id))&&this.name.equals(s.name);
}
/**
*Sometimes even though we Override the equals, but we still can not determine whether the *two objects the same,
*In the collection object, such as HashSet, this time we have to Override the hashoCode()
*/
public int hashCode(){
return id + name.hashCode() ;
}
Это не исходный код Java. Или вы создали свой собственный класс под названием 'string'? И вы действительно должны взглянуть на это: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html –
Я вижу, что Joachim изменил пару ошибок в вашем примере кода. Тем не менее, я рекомендую посмотреть ссылку, которую я опубликовал в своем предыдущем комментарии. –
Я использую класс строки java – Vidya