Object class toString () and equals () method

We know that, Object class is the parent of all classes, it is also known as the root class, ancestor. So, we take a look at two of the most commonly used method of the Object class is how to use.

1 .toString method :

toString () method of the Object class returns the object address the default value, if a class toString () method returns the address value is not an object, then this class overrides the toString () method.

public  class Test5 should be conducted {
     public  static  void main (String [] args) { 
        the Person P1 = new new the Person (); 
        System.out.println (p1.toString ()); 
        // not override toString () method returns the object address value cn.itcast.Person@3feba861 
    } 
}

Next we rewrite toString () method of Person

public class Person {
    private String name;
    private int age;

    //重写toString()方法
    @Override
    public String toString() {
        return "姓名: "+name+" 年龄:"+age;
    }

    public Person(){

    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Let's look at toString method returns a value of ():

public  class Test5 should be conducted {
     public  static  void main (String [] args) { 
        the Person P1 = new new the Person (); 
        System.out.println (p1.toString ()); 
        // rewrite toString () method, the method returns the return value of 
        the Person P2 = new new the Person ( "AAA", 18 ); 
        System.out.println (p2.toString ()); 
//         name: null Age: 0
 //         name: AAA Age: 18 
    } 
}

 

2. the equals method :

Source equals () method is as follows:

public boolean equali(Object obj){

  return this==obj;

}

Because == comparison is the address value of the object, so the Object class equals () the comparison is the default address value, needs to be rewritten to be meaningful. Specifically, how to rewrite, to see what if we compare two objects based on the same.

For example: We require that if two people's names and ages are the same, then the same two people.

    //重写Person类的equals()方法
    @Override
    public boolean equals(Object obj) {
        return this.name==((Person)obj).name&&this.age==((Person)obj).age;
    }

Again test the equals () method:

public class Test5 {
    public static void main(String[] args) {    
        Person p3=new Person("aa",21);
        Person p4=new Person("aa",23);
        Person p5=new Person("bb",21);
        Person p6=new Person("aa",21);
        System.out.println(p3.equals(p4)); //false
        System.out.println(p3.equals(p5)); //false
        System.out.println(p3.equals(p6)); //true
   }
}

Notes equals () method:

(1) except using a.equals (b) This wording may also be used Objects.equals (a, b), the effect is the same.

(2) equals null pointer security method, i.e., tolerance null pointer. I.e., equals () method parameters can be null, the return value is always false.

(3) null can not invoke methods, so if a is empty, you can not call a.equals () method, otherwise there will be NullPointException. Because of this, we will generally be constant or known non-null object on the front, that is, written "aa" .equals (str) instead str.equals ( "aa").

Guess you like

Origin www.cnblogs.com/iceywu/p/12015118.html