digression

Everything has roots. No one can get off the revolutionary boat. Yesterday, I visited my grandmother from the hospital. Old, fragile, pushing the bed shuttle between floors, looking at a variety of patients, looking at their uncomfortable appearance, the atmosphere of oppression I can hardly breathe, my mind emerges a lot of pictures, young people in the process of struggle to give yourself a little love, but also remember to take care of yourself.

Object

What is the Object class? (It’s not too much to call me dad!)

The Object class is the root of the class hierarchy. Every class has Object as its superclass. All objects, including arrays, implement methods of this class. A class that does not explicitly inherit from a parent class is a subclass of Object. Obeject is in the java.lang package. When you use it, you don’t need to guide the package.

// What does that mean? is
	class Son{}
	/ / equivalent to the
	class Son extends Object{}
Copy the code

That’s not even more dramatic, but it can also accept objects of all classes, too elaborate.

public class Demo01 {
    public static void main(String[] args) {
        Object food = new Food();
        Object meat = newMeat(); }}class Food{}
class Meat{}
Copy the code

What can it do? (Its method)

First look at the JDK’s pitch:Deep into the source code method:

	// Returns the runtime class of this Object
	public final nativeClass<? > getClass();// For hash lookup
	public native int hashCode(a);
	// Is used to indicate whether some other object is "equal" to this object
 	public boolean equals(Object obj) {
        return (this == obj);
    }
    / / protection method, the realization of a shallow copy of object, only implements the Cloneable interface to invoke the method, otherwise throw CloneNotSupportedException anomalies.
 	protected native Object clone(a) throws CloneNotSupportedException;
 	public String toString(a) {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    // This method wakes up a thread waiting on the object.
    public final native void notify(a);
    // This method wakes up all threads waiting on the object
    public final native void notifyAll(a);
    The wait method makes the current thread wait for the lock on the object. The current thread must be the owner of the object, i.e., the lock on the object. The wait() method waits until the lock is acquired or interrupted. Wait (long timeout) Sets a timeout interval and returns if the lock has not been acquired within the specified time. The thread can then be scheduled, throwing an InterruptedException if it is interrupted.
    public final void wait(a) throws InterruptedException {
        wait(0L);
    }
    public final native void wait(long timeoutMillis) throws InterruptedException;
    rotected void finalize(a) throws Throwable {}public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
        if (timeoutMillis < 0) {
            throw new IllegalArgumentException("timeoutMillis value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeoutMillis++;
        }

        wait(timeoutMillis);
    }
Copy the code

The most common thing we use is to compare and contrast toString().

If not we just print toString() which is the method, huh? In the source code, you can see that reflection gets the fully qualified class name of the current object and the @hexadecimal hash string. This is what you print without overwriting toString().

package week.third.test;

public class Demo02 {
    public static void main(String[] args) {
        Object s = new Student();
        System.out.println(s);
        //week.third.test.Student@49e4cb85}}class Student {}
Copy the code

Now let’s do it.

public class Demo01 {
    public static void main(String[] args) {
        Object person = new Person("Zhang".18);
        Object person1 = new Person("Zhang".18);
        // demo: equals(); false
        System.out.println(person.equals(person1));

        // Demo: toSting(); Person{Name=' age ', age=18}System.out.println(person.toString()); }}class Person{
    public String getName(a) {
        return Name;
    }

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

    public Person(a) {}public void setName(String name) {
        Name = name;
    }

    private String Name;

    public int getAge(a) {
        return age;
    }

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

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

    private int age;


}

Copy the code

Call equals() to compare the addresses of the two objects by default. Returns false even if the attributes of the object are the same.

The solution is to tie the bell, and we need to rewrite the comparison method, so that we can determine the attributes. After that, it returns true. Therefore, when comparing data of reference types, we should overwrite equals() first, otherwise we will still be comparing the heap memory addresses of the two objects.

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null|| getClass() ! = o.getClass())return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(Name, person.Name);
    }
Copy the code

Original is not easy, hope guest officer, give an opportunity!