The Object class is the parent of all classes in Java, where each class is extended by the Object class. Therefore, instance methods in Object are also available to all Java objects.
public final native Class<? > getClass();
This method is used to get the type of the runtime. This method returns the run-time Class of this Object. Class objects can also be obtained using object.class or class.forname ()
public native int hashCode();
This method returns the hash value of the current object, which is the physical address of the object, in the range -2^31 to 2^ 31-1. It is usually overridden with equals() to ensure that equal objects have equal Hashcodes. The Java specification has the following constraints on hashCode:
- Multiple calls to the same object
hashCode()
Method must consistently return the same integer if the object is carried outequals()
The information used in the comparison has not been modified - If two objects
x.equals(y)
Method returns true, then the value of x and yhashCode()
Must be equal - If two objects
x.equals(y)
The x and y method returns falsehashCode()
It can be equal or unequal. However, generating different integer results for unequal objects can improve the performance of a hash table. - The default
hashCode()
Is the hash value that converts the memory address to, overwrite after is custom computation way. Also throughSystem.identityHashCode(Object o)
To return the originalhashCode()
.
public boolean equals(Object o)
This method is used to compare whether the contents of two objects are equal. By default, it compares whether references refer to the same object, unless overridden by subclasses.
public boolean equals(Object obj) {
return (this == obj);
}
Copy the code
There are several conventions to follow when overriding equals() :
- Reflexivity: i.e
x.equals(x)
Return true, x is not null - Symmetry: i.e.
x.equals(y)
与y.equals(x)
X and y are not null - Transitivity: i.e
x.equals(y)
The result is true,y.equals(z)
If the result is true, thenx.equals(z)
The result must also be true - Consistency: i.e.
x.equals(y)
Returns true or false, unchangedequals()
Method must also return consistent results from multiple calls. X and y are not null - If x is not null,
x.equals(null)
Returns false
protected native Object clone() throws CloneNotSupportedException;
This method returns a copy of the current object. Note that the clone() method in the Object class is a shallow copy. This is a protected method for subclass overrides. But you need to implement Cloneable interface, which is a marker interface, if not implemented, when the Object. The clone () method, which will throw CloneNotSupportedException anomalies, as follows:
public class CloneTest implements Cloneable {
private int value;
@Override
protected CloneTest clone(a) throws CloneNotSupportedException {
return (CloneTest) super.clone(); }}Copy the code
public String toString()
Returns a string representation of the object, the default toString() method, which simply takes the current class’s full class name + @ + hexadecimal hashCode() value.
public final void wait() throws InterruptedException
There are also two overloaded versions of this method:
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException;
These three methods are used for communication between threads. They block the current thread and wait for another thread to call notify()/notifyAll() to wake it up. All three methods are final and cannot be overridden by subclasses.
Some considerations for calling wait() :
wait()
Method can only be called after the current thread has acquired the lock monitor for the object, otherwise it will be thrownIllegalMonitorStateException
The exception.- call
wait()
Method, the thread releases the lock monitor. whileThread.sleep()
.Thread.yield()
The lock is not released. wait()
Method blocks until another thread calls the current object’snotify()
/notifyAll()
Method to wake it up. whilewait(long timeout)
Is to wait for a given timeout period (in milliseconds), if not already callednotify()
/nofiyAll()
It wakes up automatically.wait(long timeout, int nanos)
If the second argument is greater than 0 and less than 999999, the first argument +1 is used as the timeout.
public final native void notify();
Randomly wake up a thread that previously called wait() on the current object.
public final native void notifyAll();
Wake up all threads that previously called wait() on the current object. Why wait()/notify() in Object? Because each Object can be a lock monitor Object, all objects are available when placed in Object.
protected void finalize() throws Throwable { }
This method is called by the JVM to clean up resources before garbage collection. This approach may reposition the object to a reachable state, making garbage collection impossible for the JVM. This approach requires the following:
- Never actively call an object
finalize()
Method that is invoked by the garbage collection mechanism itself finalize()
There is uncertainty about when and if it will be called- When the JVM executes the recoverable object
finalize()
This object may be returned to a reachable state - When the JVM to perform
finalize()
Method, the garbage collection mechanism does not report the exception and the program continues