A list,

The Object class is the base class for all other classes in Java. The Object class has no defined attributes, and there are 12 methods.

Two, the main methods

1, Class constructor public Object();

In Java, a constructor without a constructor is provided by default during class definition. So the Object base class naturally also has a no-parameter constructor.

Pay attention to

All classes have constructors, and classes that do not define constructors default to a no-argument constructor. But not all class constructors are public.Copy the code

2, private static native void registerNatives();

The registerNatives function has native keyword modification. The native modification method is a local method, not a Java method, which is usually implemented by C/C++ and called by Java. And this method is. The main function of this method is to map the C/C++ implemented methods to the Java Nativa methods. This method is implemented in the static code block immediately following it

static {
    registerNatives();
}
Copy the code

3, public final native Class<? > getClass();

GetClass () is also a native method that returns the Object’s Class/runtime Class<? >. The effect is the same as object.class.

Class: An abstraction of instances that share the same characteristics and behavior

Object: A concrete instance of a class

Class: An abstraction of classes, such as classes with package names, attributes, superclasses, methods, etc.

public native int hashCode(); ,

Convention: 1. The hashCode value of an object is the same during a single run of a Java application, but the hashCode value for multiple runs does not need to be the same. Hashcode equals() ==>hashcode equals() ==>

public boolean equals(Object obj);

Let’s take a look at the implementation of the methods in the Object class

public boolean equals(Object obj) {
    return (this == obj);
}
Copy the code

== means that the values of variables are identical (for base types, addresses store values, and reference types store addresses to actual objects), so why are there so many differences between equals and ==? Let’s look at a couple of scenarios

Public Boolean equals(Object anObject) {if (this == anObject) {return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- ! = 0) { if (v1[i] ! = v2[i]) return false; i++; } return true; } } return false; }Copy the code
Public Boolean equals(Object obj) {if (obj instanceof Long) {return value == ((Long)obj).longValue(); } return false; }Copy the code

As you can see, the basic types we use a lot are overriding equals, essentially comparing actual values. In general, equals means that the contents of the objects are identical, which refers to the characteristics/attributes of the objects. We also adhere to this convention when overriding equals.

protected native Object clone() throws CloneNotSupportedException;

As you can see, the Clone method is native, not implemented in Java, but in C/C++. Unlike other native methods, which can be called directly, the Clone method cannot be called directly and must implement the Cloneable interface. The Clone function returns a reference to the new Clone object, which occupies a different amount of heap space from the original.

Without rewriting the Clone method, the property value of the cloned object will not be changed if the property value of the original object is changed.

public String toString();

Source:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Copy the code

The default is to use the class name @hashCode. The toString() method returns a string representation of the object, which we usually override to print some core information about the object.

public final native void notify();

Wait /notify/notifyAll methods are mainly used for collaboration between Java multithreads.

Wait () : wait(long timeout)/wait(long timeout, int nanos) : The current thread in which this method is called waits until the notisfy()/notisfyAll() methods of this method are called on another thread, or the specified amount of notify()/notifyAll() timeout is exceeded: wakes up the single thread/all threads waiting on the monitor of this object

public final native void notifyAll();

Same as above

public final native void wait(long timeout) throws InterruptedException;

Same as above

public final void wait(long timeout, int nanos) throws InterruptedException

Same as above

public final void wait() throws InterruptedException

Same as above

protected void finalize() throws Throwable

protected void finalize() throws Throwable { }
Copy the code

As you can see, this method is an empty method with no execution logic. So why is this method defined on Object? It is because all objects have finalize behavior, which is implemented before garbage collection. It is not called by us actively.

Third, the interview

The only condition to compare whether two objects are equal is equals.

The hashCode method is used to enhance the performance of hash tables. In the Set class, take Set as an example. When a new object is added, it is necessary to determine whether there are already objects equal to this object in the existing Set. If there is no hashCode() method, it is necessary to traverse the Set once and use equals() method to determine whether the two objects are equal one by one. By means of hasCode method, first calculate the hash code of the object to be added, and then calculate the position of the object according to the hash algorithm, and directly determine whether there is an object in this position. (Note: The underlying Set is implemented using the principle of Map.)Copy the code

Four, thinking