The object class ToString method overrides the Equals method override

Source: Internet
Author: User

The

Object class has the ToString method, and the Equals method
ToString () Overrides:
Equals () method:
Double equals comparison is, is the same object
equals, two objects are not like a

Package test;
    public class Teacher {private String name;


    private int age;
    /** * Rewrite tostring */public String tostring () {return name; 
        }/** * Overrides equals * is used to determine if the current object is "as long as the given object" * compared to the content (property name) */public Boolean equals (object obj) {
        if (obj = = null) {//passed in object is null return false;
        } if (obj = = this) {//pass in the object whether it is itself return true;
            The IF (obj instanceof Teacher) {//passed in object is the same type of Teacher t = (Teacher) obj;
        Return T.name.equals (this.name) && t.age = = This.age;
    } return false;
        } public static void Main (string[] args) {Teacher tea = new Teacher ();
        Tea.name = "Wang";
        Tea.age = 31;
        System.out.println (tea);//wang println () calls the ToString method Teacher tea1 = new Teacher ();
        Tea1.name = "Wang";
        Tea1.age = 31; System.out.println (tea==TEA1);//false double equals comparison is whether the same object System.out.println (Tea.equals (TEA1));//true}}

 

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.