Java's API (API: Application (application) Programming (program) Interface (Interface))

Press ctrl Click to view the source code;

A, equals method:

1. Comparative whether two objects are the same.

2.Objec class, if the reference data type on both sides, the memory address comparing two objects.

3. objec class, if both sides of numeric type, compares the contents of two objects.

4. Use of a comparison operator ==.

5.equals rewrite:

Code:

public class Person {
    private String name;
    private int age;
    
    public Person(){
        super();
    }
    
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    // override equals method to compare the contents of 
    public  Boolean equals (Object obj) {
         // robust type determination 
        IF (obj == null ) {
             return  to false ;
        }
        if(obj==this){
            return true;
        }
        // foundation Analyzing 
        IF (obj the instanceof the Person) {
            P the Person = (the Person) obj;
             // reference data types with comparisons are equals, not ==; 
            return  the this .name.equals (p.name) && the this .age == p.age;
        }
        return false;
    }
}

 

Two, toString method: Back to the current object

1. Objec class , toString method returns the result of the memory address.

2. When the direct printing reference data types, in fact, calls the toString method.

3. rewrite toString method:

    // override toString method 
    public String toString () {

        return name+" "+age;
    }

Guess you like

Origin www.cnblogs.com/l1314/p/12076903.html