Java class notes------tostring Method and Equals method

Source: Internet
Author: User

*overriding the ToString method*when we need to use the ToString method of the current class, we usually*you will need to override this method. The format of the specific return string is not strictly*requirements, depending on the needs of the actual development in the future. But usually anti-*The property value of the current class should be included in the returned string to understand the current*the information for the object. *format: (x, y) PublicString toString () {return"(" + x + "," + y + ")"; }             *usually we rewrite the Equals method of a class. *The purpose of the Equals method is to compare the current object to the given object's*whether the content is the same (property value)* If you do not rewrite it is provided using object, and its internal is "= =" *realized, so it makes no sense. *the Equals method does not necessarily require that the content of two objects be more consistent*all property values are the same, depending on the actual demand.  Public Booleanequals (Object obj) {if(obj = =NULL){        return false; }      if(obj = = This){        return true; }      if(objinstanceofPoint ) {Point P=(point) obj; return  This. x==p.x && This. y==p.y; }      return false; }                 * "=="when comparing two reference types, the comparison is the same*object System.out.println (P= = p1);//false* "Equals"when comparing two reference types, the content is compared*is the same.         (Not like) System.out.println (P.equals (p1));//true    

Java class notes------tostring Method and Equals method

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.