原始碼版本:JDK8
java.lang.Object
注釋
/*
* Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang;
這段就不用說了,只提一次,Oracle的著作權,進入正文
/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author unascribed
* @see java.lang.Class
* @since JDK1.0
*/
Object類是所有類的父類,就像祖師爺,所有的物件包括陣列都實作這個類的方法,也因為所有類都默認繼承Object,所以省略了extends Object關鍵字
registerNatives()
private static native void registerNatives();
static {
registerNatives();
}
主要作用是將C/C++中的方法映射到Java中的native方法,實作方法命名的解耦,函式的執行是在靜態代碼塊中執行的,在類首次進行加載的時候執行,
getClass()
public final native Class<?> getClass();
回傳此Object的運行時類,回傳的類物件是被表示類的static synchronized方法鎖定的物件,
實際結果的型別是Class<? extends |X|>其中|X|是靜態型別上其表達的擦除getClass被呼叫,
例如,在此代碼片段中不需要轉換:
Number n = 0;
Class<? extends Number> c = n.getClass();
結果
表示類物件的運行時類的Class物件,
hashCode()
public native int hashCode();
回傳物件的哈希碼值,
支持這種方法是為了散串列,如HashMap提供的那樣,
hashCode的總合同是:
只要在執行Java應用程式時多次在同一個物件上呼叫該方法,hashCode方法必須始侄訓傳相同的整數,前提是修改了物件中equals比較中的資訊,
該整數不需要從一個應用程式的執行到相同應用程式的另一個執行保持一致,
如果根據equals(Object)方法兩個物件相等,則在兩個物件中的每個物件上呼叫hashCode方法必須產生相同的整數結果,
不要求如果兩個物件根據equals(java.lang.Object)方法不相等,那么在兩個物件中的每個物件上呼叫hashCode方法必須產生不同的整數結果,
但是,程式員應該意識到,為不等物件生成不同的整數結果可能會提高哈希表的性能,
盡可能多的合理實用,由類別Object定義的hashCode方法確實為不同物件回傳不同的整數, (這通常通過將物件的內部地址轉換為整數來實作,但Java的編程語言不需要此實作技術,)
equals(Object obj)
public boolean equals(Object obj) {
return (this == obj);
}
clone()
protected native Object clone() throws CloneNotSupportedException;
toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
notify()
public final native void notify();
notifyAll()
public final native void notifyAll();
wait(long timeout)
public final native void wait(long timeout) throws InterruptedException;
wait(long timeout, int nanos)
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);
}
wait()
public final void wait() throws InterruptedException {
wait(0);
}
finalize()
protected void finalize() throws Throwable { }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253807.html
標籤:Java
