Object class

1. equals()

/* * the difference between equals() and the == operator * * 1, == : operator * 1.1 can be used on base and reference datatype * 1.2 if the base datatype being compared: Compare two basic datatype variables to see if they hold the same data (not necessarily of the same type) * 1.3 If you compare reference datatype: Compare two objects to see if the address value is the same. Whether two references refer to the same object entity * * 2, use of equals() * 1. Public Boolean equals(Object obj) {return (this == obj); public Boolean equals(Object obj) {return (this == obj); } equals() equals() equals() equals() equals() equals() == Things like String, Date, File, and wrapper classes override equals() in Object. When overridden, instead of comparing the address of two references, we compare the "entity content" of two objects. * * 5. In general, our custom classes use equals() to compare whether the "entity contents" of two objects are the same. We need to override equals() in the Object class
Copy the code

2. toString()

When we print a reference to an Object, we actually call the current Object's toString(), returning the class name and its reference address * * 2.  * public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } * * 3. Classes like String, Date, File, and wrapper override the toString() method of the Object class. * Causes the "entity content" information to be returned when the object's toString() is called * * 4. Custom classes can also override the toString() method, which returns the "entity content" of the object */ when called
Copy the code
// Use the equals() and toString() methods for example:
public class T1equalsAndtoString {

    @Test
    public void test1(a){

        // Basic data type
        int i = 10;
        int j = 10;
        double d = 10.0;
        char c = 10;
        System.out.println(i == c);//true
        System.out.println(i == j);//true
        System.out.println(i == d);//true
        char c1 = 'A';
        int c2 = 65;
        System.out.println(c1 == c2);//true
        System.out.println();


        // Reference the data type
        Customer cust1 = new Customer("abc".12);
        Customer cust2 = new Customer("abc".12);
        System.out.println(cust1 == cust2);//false
        System.out.println(cust1.equals(cust2));// Override equals to compare entity content. true
        System.out.println();


        // Things like String, Date, File, and wrapper classes override equals() in Object
        Date date1 = new Date(32432525324L);
        Date date2 = new Date(32432525324L);
        System.out.println(date1.equals(date2));
        System.out.println();


        Customer cust3 = new Customer("Tom".21);
// 1. When we print a reference to an object, we actually call toString() of the current object, returning the class name and its reference address
        System.out.println(cust3.toString());//com.atguigu.java1.Customer@15db9742-->Customer[name = Tom,age = 21]
        System.out.println(cust3);//com.atguigu.java1.Customer@15db9742-->Customer[name = Tom,age = 21]

        String str = new String("MM");
        System.out.println(str);//MM

        Date date = new Date(4534534534543L);
        System.out.println(date.toString());//Mon Sep 11 08:55:34 GMT+08:00 2113
    }


class Customer{

    private String name;
    private int age;

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


    public String getName(a) {
        return name;
    }

    public int getAge(a) {
        return age;
    }


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

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


// // automatically implements overrides to equals() : CTRL + Shift + S shortcut
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (! (o instanceof Customer)) return false;
// Customer customer = (Customer) o;
// return getAge() == customer.getAge() &&
// Objects.equals(getName(), customer.getName());
/ /}

    // automatically implement toString()
// @Override
// public String toString() {
// return "Customer{" +
// "name='" + name + '\'' +
// ", age=" + age +
/ / '} ';
/ /}

    / / rewrite the equals ()
    // Overriding principle: compare two objects' entity contents (i.e., name and age) to see if they are the same
    @Override
    public boolean equals(Object obj) {
        //1. Check whether the address value is the same, if the same object, then the entity content must be the same
        if(this == obj){
            return true;
        }

        if(obj instanceof Customer){
            Customer cust = (Customer)obj;
            // Compare whether each attribute of two objects is the same
            //age is the basic data type, compared with ==
            //name is a String, compared with equals in String
            if(cust.age == this.age && cust.name.equals(this.name)){
                return true;
            }else {
                return false; }}else {
            return false; }}}Copy the code

3. hashCode()

It says in the set…

4. clone()

  • Shallow copy: When copying an object, only the object itself and its basic variables are copied, without copying the object referred to by the reference contained in the object.
  • Deep copy: The deep copy not only copies the object itself, but also all objects that the object contains references to.
/* * 1. Object assignment: two references to the same object * * 2. Clone () shallow copy: The clone() method in Object is protected and returns the Object's * 2.1 implementation * 2.1.1 implementation Cloneable interface, which is a tag interface and has no methods of its own. * 2.1.2 Overwriting the Clone () method, visibility is increased to public * 2.2 Features: Shallow copy only copies the object itself and the basic variables in the object, but does not copy the object containing references to the object * * 3. Clone () deep copy * * 4. The shallow copy example is as follows: */

public class T3clone {

    @Test
    Object assignment: Two references refer to the same object
    public void test1(a) throws Exception {
        Person p1 = new Person("abc".10);
        Person p2 = p1;
        System.out.println(p1 == p2);//true
        p1.setAge(19);
        System.out.println(p2.toString());//Person{name='abc', age=19, address=null}
        System.out.println(p1.toString());//Person{name='abc', age=19, address=null}
    }

    @Test
    // Shallow copy refers to copying only the object itself and the basic variables in the object
    public void test2(a) throws Exception{

        Person p1 = new Person("abc".10);
        Person p3 = (Person) p1.clone();
        System.out.println(p3 == p1);//false
        p3.setName("efg");
        System.out.println(p3.toString());//Person{name='efg', age=10, address=null}
        System.out.println(p1.toString());//Person{name='abc', age=10, address=null}
    }

    @Test
    // Without copying the object containing the reference to the object
    public void teat3(a) throws Exception{

        Address address = new Address("north".110.32);
        Person p4 = new Person("Millet".24, address);
        Person p5 = (Person)p4.clone();
        p5.setAddress(new Address("east".102));
        System.out.println(p4 == p5);//false
        System.out.println(p4.toString());//Person{name=' mi ', age=24, address= address {location='north', distance=110.32}}
        System.out.println(p5.toString());//Person{name=' mi ', age=24, address= address {location='east', distance=102.0}}}}class Person implements Cloneable {

    private String name;
    private int age;
    private Address address;

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

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

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getAddress(a) {
        return address;
    }

    public String getName(a) {
        return name;
    }

    public int getAge(a) {
        return age;
    }

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

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


    @Override
    public String toString(a) {
        return "Person{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                ", address=" + address +
                '} ';
    }

    @Override
    protected Object clone(a) throws CloneNotSupportedException {
        return super.clone(); }}class Address{

    private String location;
    private double distance;

    public String getLocation(a) {
        return location;
    }

    public double getDistance(a) {
        return distance;
    }

    public Address(String location, double distance) {
        this.location = location;
        this.distance = distance;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    @Override
    public String toString(a) {
        return "Address{" +
                "location='" + location + '\' ' +
                ", distance=" + distance +
                '} '; }}Copy the code