JDK8原始碼閱讀筆記--------java.lang.Object
Object:
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
是所有類的超類。
1.getClass():
Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
2.hashCode():
Returns a hash code value for the object.
3.equals(Object obj):
4.clone():
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object.
意思是:準確的說克隆的意思是,得到類的class和原來類的class是一樣的,如下:
public static void main(String[] args) { ArrayList arrayList = new ArrayList(); //false System.out.println(arrayList == arrayList.clone()); //true System.out.println(arrayList.getClass() == arrayList.clone().getClass()); //true System.out.println(arrayList.clone().equals(arrayList)); }
5.toString(): Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. 推薦所有子類重寫這個方法。 預設返回:getClass().getName() + "@" + Integer.toHexString(hashCode());
public static void main(String[] args) {
Object o = new Object();
//[email protected]
System.out.println(o.toString());
}
6.notify():
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.
7.notifyAll():
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.
8.wait()、wait(long timeout):
Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
9.finalize():
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
意思是當GC確定這個物件沒有任何引用時候,由gc呼叫這個方法。子類重寫finalize()方法來中斷系統資源或者做清理。