Of course not. The hashcode and equals methods can be rewritten. If one of them is overwritten, but the other is not overwritten, this conclusion is obviously incorrect.
The Code is as follows:
Public class test {<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> Test2 T = new Test2 ("zhangsan", 20 ); <br/> Test2 t2 = new Test2 ("zhangsan", 30); </P> <p> system. out. println (T. equals (T2); <br/> system. out. println (T. hashcode () = t2.hashcode (); <br/>}</P> <p> class Test2 {<br/> Public Test2 (string name, int age) {<br/> super (); <br/> This. name = Name; <B R/> This. age = age; <br/>}</P> <p> @ override <br/> Public Boolean equals (Object OBJ) {<br/> If (this = OBJ) <br/> return true; <br/> If (OBJ = NULL) <br/> return false; <br/> If (getclass ()! = Obj. getclass () <br/> return false; <br/> Test2 Other = (Test2) OBJ; <br/> If (name = NULL) {<br/> If (Other. name! = NULL) <br/> return false; <br/>} else if (! Name. equals (Other. name) <br/> return false; <br/> return true; <br/>}</P> <p> string name = ""; <br/> int age; </P> <p>}
The running result is as follows:
True <br/> false
Of course, we 'd better rewrite the hashcode method when rewriting the equals method. The Code is as follows:
Public class test {<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> Test2 T = new Test2 ("zhangsan", 20 ); <br/> Test2 t2 = new Test2 ("zhangsan", 30); </P> <p> system. out. println (T. equals (T2); <br/> system. out. println (T. hashcode () = t2.hashcode (); <br/>}</P> <p> class Test2 {<br/> Public Test2 (string name, int age) {<br/> super (); <br/> This. name = Name; <B R/> This. age = age; <br/>}</P> <p> @ override <br/> Public int hashcode () {<br/> final int prime = 31; <br/> int result = 1; <br/> result = prime * result + (name = NULL )? 0: Name. hashcode (); <br/> return result; <br/>}</P> <p> @ override <br/> Public Boolean equals (Object OBJ) {<br/> If (this = OBJ) <br/> return true; <br/> If (OBJ = NULL) <br/> return false; <br/> If (getclass ()! = Obj. getclass () <br/> return false; <br/> Test2 Other = (Test2) OBJ; <br/> If (name = NULL) {<br/> If (Other. name! = NULL) <br/> return false; <br/>} else if (! Name. equals (Other. name) <br/> return false; <br/> return true; <br/>}</P> <p> string name = ""; <br/> int age; </P> <p>}
In this case, the answer to the question is correct.