Click “like” to see, form a habit, the public account search [dime technology] pay attention to more original technical articles. This article has been included in GitHub org_Hejianhui /JavaStudy.
preface
Java is an object-oriented language. In Java, everything can be regarded as an Object, and all objects in Java inherit from the Object class by default.
This article is based on JDK1.8
You can see that there are 12 methods in total, of which registerNatives() is a native modification, a native method, which is implemented in DLL using C(C++) and then called via JNI.
getClass
public final nativeClass<? > getClass();Copy the code
GetClass is a native method that returns the runtime Class of an object. Its return value is Class c = obj.getClass(); With object C, we can get all the member methods of that object, each of which is a Method object; We can also get all the member variables of the object, each of which is a Field object; Similarly, we can get the object’s Constructor, which is a Constructor object. This method is often used in reflection.
hashCode
public native int hashCode(a);
Copy the code
- The hashCode method returns a hash value.
- The return value is translated from the address of the object by default.
- Calls to hashCode from the same object return the same value.
- If two objects are equal to equals, then hashCode must be equal.
- The equals of two objects are not equal, so hashCode is not necessarily equal.
equals
public boolean equals(Object obj) {
return (this == obj);
}
Copy the code
Equals is a very simple implementation that compares whether two objects are equal based on their memory addresses. In addition, equals follows the following principles:
1, reflexivity: x.equals(x);// true
2, symmetry: x.equals(y) == y.equals(x);// true
3, transitivity:if (x.equals(y) && y.equals(z))
x.equals(z); // true;
4X.equials (y) == x.equials (y);// true
5, non - void, for any notnullThe object x of the.nullThe results are bothfalse: x.equals (null); // false;
Copy the code
Why override hashCode and equals?
Since both methods are related to comparing objects, if you want to do object comparison in your program, you will probably have to rewrite them. Since the default equals comparison logic is to compare the addresses of the objects, the memory addresses of the two objects must be different, so the eqals comparison between two objects must return false anyway.
In real programming, however, we often encounter de-duplication, either by putting objects into an ordered collection or by storing objects into a non-repeating collection, where we can’t do this without overriding equals and hashCode.
For example, there are two objects in the system at the same time, object A and object B, whose names and ID numbers are identical. At this point, there are two objects in system memory, but their contents are consistent. One person has produced two duplicate messages at the same time. If the default equals method is used, the two objects are never equal and can never be compared to the same duplicate information. So, override the equals and hashCode methods to do this.
clone
protected native Object clone(a) throws CloneNotSupportedException;
Copy the code
Clone () is a protected method of Object, which is not public. If a class does not explicitly override clone(), other classes cannot directly call the clone() method of that class instance. In addition, the Clone notes several important points:
- The cloned object must implement Cloneable interface and rewrite clone method, otherwise will quote CloneNotSupportedException anomalies
- The clone() method is not a Cloneable interface method, but a protected method of Object. Cloneable interface just rules, if a class does not implement the Cloneable interface again call clone () method, which will throw CloneNotSupportedException.
- Shallow copy: The reference type of the copied object refers to the same object as the original object.
- Deep copy: The reference types of the copied object and the original object refer to different objects.
We will discuss shallow and deep copies later.
toString
public String toString(a) {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Copy the code
Returns a string representation of the object, a very important method
- getClass().getName(); Get the full path name of the bytecode file such as java.lang.object;
- Integer.toHexString(hashCode()); Converts the hash value to a string in hexadecimal format.
Wait and notify
public final void wait(a) throws InterruptedException {
wait(0);
}
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
Copy the code
Wait () also causes the current thread to release the lock it holds. Until another thread calls notify() or notifyAll() of this object, the current thread is awakened to the ready state.
Wait (long timeout) allows the current thread to wait(block) until another thread calls notify() or notifyAll() of the object, or until the specified amount of time has elapsed and the current thread is awakened to ready.
Wait (long timeout, int nanos) functions the same as wait(long timeout), the only difference being that this can provide higher precision. The total timeout time (in nanoseconds) is 1000000 x timeout+ nanos. By the way, wait(0,0) has the same effect as wait(0).
public final native void notify(a);
public final native void notifyAll(a);
Copy the code
NotifyAll is a notifyAll that wakes up all the threads in the wait queue.
Note: Specifications for notify and wait methods. This means that both must be used in synchronized modified synchronized methods or synchronized code.
What is the difference between thread.sleep () and Object.wait()?
First, both can suspend the current thread, freeing CPU control. The main difference is that object.wait () frees control of the Object lock while releasing the CPU. Thread.sleep() does not release the lock. In other words, to sleep is to be a dog in the manger.
The complete code
package java.lang;
public class Object {
/** * a native method implemented in DLL in C(C++) and then called by JNI */
private static native void registerNatives(a);
/** * This method is called automatically when the object is initialized
static {
registerNatives();
}
/** * returns the runtime class */ for this Object
public final nativeClass<? > getClass();/** * The general convention for hashCode is: * 1. The same integer must be consistently returned when the hashCode() method is called on the same object multiple times during the execution of a Java application, provided that the information used to compare the objects to equals has not been modified. * The integer need not be consistent from one execution of an application to another execution of the same application. * 2. If two objects are equal according to equals(object), then calling the hashCode method on each of them must produce the same integer result. * 3. If two objects are not equal according to equals(java.lang.object), then calling hashCode() on either Object does not require that different integer results be generated. * However, programmers should be aware that generating different integer results for unequal objects can improve hash table performance. * /
public native int hashCode(a);
/** ** the memory address of the object */
public boolean equals(Object obj) {
return (this == obj);
}
/** * The local clone method is used to copy objects */
protected native Object clone(a) throws CloneNotSupportedException;
/** * returns a string representation of the object, the very important method * getClass().getName(); Get the full path name of the bytecode file such as java.lang.object * integer.tohexString (hashCode()); Converts the hash value to a string in hexadecimal format. * /
public String toString(a) {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
/** * cannot be overridden to wake up a thread that is waiting or time_wait for the object. This method can only be called in a synchronized method or block
public final native void notify(a);
/** * cannot be overridden to wake up all threads that are waiting or time_waiting for the object. This method can only be called from a synchronized method or block
public final native void notifyAll(a);
/** * cannot be overridden in a thread call, causing the current thread to enter a time_waiting state. Timeout is in milliseconds. This method can only be called in a synchronized method or block
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
/** * Causes the current thread to wait until another thread calls notify() or notifyAll() on this object. In other words, this method behaves as if it only performs a wait(0) call. * The current thread must own this object monitor. * The thread claims ownership of the monitor and waits until another thread notifies the thread waiting on the monitor of this object by calling notify or notifyAll. * Then the thread will wait until it regains ownership of the monitor before resuming execution. * /
public final void wait(a) throws InterruptedException {
wait(0);
}
/** * This method is used when objects are collected. This method is supported by the JVM. The Finalize method of objects does nothing by default. * /
protected void finalize(a) throws Throwable {}}Copy the code
PS: There is a technical exchange group (QQ group :1158819530), which is convenient for everyone to communicate with each other, continue to learn, and make progress together. If you need it, you can add it.
GitHub Org_Hejianhui /JavaStudy GitHub Hejianhui /JavaStudy