Java tip [010]: when the object A. Equals (B), A. hashcode = B. hashcode?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.