1. 程式人生 > >【java SE】java 原始碼閱讀 —— Object

【java SE】java 原始碼閱讀 —— Object

看原始碼也不太懂怎麼看,Object類的 java 程式碼很少,方法實現都靠 C/C++ ,主要看註釋,然後自己理解。有不對的地方請指正

1. 概覽

import jdk.internal.HotSpotIntrinsicCandidate;

/**
 * native 關鍵字修飾的方法:
 * 1. 沒有方法體
 * 2. 將呼叫C/C++實現的方法(可以搜尋一下JNI)
 */
public class Object {

    /**
     * 這個方法使JVM能找到本地的方法(C/C++實現的方法)
     */
    private static native
void registerNatives(); static { registerNatives(); } /** * 空構 */ @HotSpotIntrinsicCandidate public Object() {} /** * 關鍵字 native, 呼叫的是 C/C++ 實現的方法 * 關鍵字 final,不允許被子類重寫 * 獲得該物件執行時的Class類的例項 * */ @HotSpotIntrinsicCandidate
public final native Class<?> getClass(); /** * 關鍵字 native * 返回 物件的雜湊值 * 允許重寫(常被重寫) */ @HotSpotIntrinsicCandidate public native int hashCode(); /** *判斷兩個物件是否相等 *常被重寫 */ public boolean equals(java.lang.Object obj) {
return (this == obj); } /** * 建立並返回物件的一個副本(拷貝) * 關鍵字 native * 關鍵字 protected */ @HotSpotIntrinsicCandidate protected native java.lang.Object clone() throws CloneNotSupportedException; /** * */ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } /** * 喚醒一個正在等待該資源的執行緒 * 關鍵字 final * 關鍵字 native */ @HotSpotIntrinsicCandidate public final native void notify(); /** * 喚醒所有正在等待該資源的執行緒 * 關鍵字 native * 關鍵字 final */ @HotSpotIntrinsicCandidate public final native void notifyAll(); /** * 使當前執行緒一直等待,直到它被喚醒. 通常 被 notify 或者 被 interrupt * 關鍵字final * 呼叫 wait(0L) */ public final void wait() throws InterruptedException { wait(0L); } /** * 使當前執行緒一直等待,直到它被喚醒. 通常 被 notify 或者 被 interrupt,或者超時 * 關鍵字 final * 關鍵字 native */ public final native void wait(long timeout) throws InterruptedException; /** * 使當前執行緒一直等待,直到它被喚醒. 通常 被 notify 或者 被 interrupt,或者超時 * 關鍵字 final * 引數 nanos 是隻納秒, 很蛋疼的是隻要 0 < nanos <= 999999, timeout ++ , 也就是說根本沒法精確到納秒的級別 */ 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); } /** * 垃圾回收時呼叫 */ @Deprecated(since="9") protected void finalize() throws Throwable { } }

2. 關於Object類: Object 類是所有類的父類, 所有類,包括陣列,都實現了該類的方法。

3. getClass() 方法

public final native Class<?> getClass();

  a. 該方法通過 final 關鍵字修飾,不允許被重寫

  b. 該方法通過 native 關鍵字修飾, 沒有方法體,具體功能由 C/C++ 實現

  c. 該方法返回一個 Class 類的例項。

    The returned {@code Class} object is the object that is locked by {@code static synchronized} methods of the represented class. 這句話不太理解,好像是說 返回的 Class類的例項 是該物件所代表的類的靜態同步方法(static synchronized methods)鎖定的物件

  d. 實際返回的型別是  Class<? extends |X|> ,|X| 是指描述該物件的類的擦除型別,英文水平有限。。直接看例子

Number n = 0;
Class<? extends Number> nClass = n.getClass();
System.out.println(nClass); // class java.lang.Integer

  |X| 是指 Number 類

4. hashcode() 方法

public native int hashCode();

  a. 該方法返回物件的雜湊值

  b. 關鍵字 native

  c. hashcode 的通俗約定:

    1)在同一個 java 程式實現中,對於同一個物件,只要物件 用於 equals 方法比較的資訊沒被修改, 不管何時呼叫 hashcode 方法都應該返回相同的值。

    2)對於不同的實現,對同一個物件,hashcode 不必返回相同的值。

    3)如果兩個物件在使用 equals 方法比較時返回 true,則它們呼叫 hashcode 方法必須返回相同的值。

    4)如果兩個物件在使用 equals 方法時返回 false, 則它們呼叫 hashcode 方法沒有要求必須返回不同的整數值。但是在設計 hashcode 演算法應該儘量返回不同的值(降低 hash 碰撞的概率);

    5)  事實上, Object 實現的 hashcode 方法也沒有實現 4)情況的預期。

5. equals(Object obj) 方法

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

  a. 該方法用於判斷兩個物件是否相等

  b. 預設的實現等同於 == 操作符

  c. equals 方法約定:

    1)自反性:對任何一個非空物件 x, x.equals(x) 返回 true

    2)  對稱性:對於任意兩個非空物件 x 和 y。 如果 x.equals(y) 返回 true,那麼  y.equals(x) 必須返回 true。

    3)傳遞性,對於任何非空物件 x、y、z, 如果 x.equals(y), y.equals(z) 都返回 true, 那麼 x.equals(z) 必須返回 true。

    4)一致性:對於任何非空物件x, y,只要在這個過程中 x,y沒有做任何修改,x.equals(y) 返回的值必須相同。

    5)對於任何非空物件 x, x.equals(null) 必須返回 false。

  d. 重寫 equals 方法的同時,必須重寫 hashcode 方法以保證 equals 為 true 的兩個物件呼叫 hashcode 方法必須相等。

6. clone() 方法

protected native Object clone() throws CloneNotSupportedException;

  b. 返回該物件的一個副本(拷貝)

  a. native 關鍵字

  c. 對任意非空物件 x:

    1)  x.clone() != x 的結果是 true

    2) x.clone().getClass() == x.getClass()的結果不一定為 true。 很明顯,因為這個方法定義返回的是 Object, 理論上你可以自己在重寫的時候返回任何類的例項,但是一般都會返回同一個類的例項。

    3)x.clone().equals(x) 也沒有要求必須返回 true

    4) 按照約定,返回的物件應該是呼叫 super.clone(), 如果一個類和它的所有父類都遵循這個約定(即都沒有重寫過 clone() 方法),x.clone().getClass() == x.getClass() 會返回 true.

    5) 按照約定,返回的物件必須是獨立於被 clone 物件的(不要返回它本身,不然就沒有意義)。 注意對 clone 物件裡的可變引用欄位的處理。

    6)如果自定義類沒有實現 Cloneable 介面, 呼叫 clone 方法將丟擲 CloneNotSupportedException

    7)所有陣列都實現了 Cloneable 介面並且可以呼叫 clone 方法。需要注意的是 陣列的 clone 方法的實現是一個 “淺拷貝”,不是”深拷貝“。其實本質上還是 值傳遞和地址傳遞的區別。看以下測試程式碼

import java.util.Arrays;

public class CloneTest {

    public static void main(String[] args) throws CloneNotSupportedException {

        //測試陣列 clone
        int[] nums = {0, 1, 2, 3, 4, 5};
        int[] cnums = nums.clone();
        System.out.println(Arrays.toString(nums));  //[0, 1, 2, 3, 4, 5]
        System.out.println(Arrays.toString(cnums)); //[0, 1, 2, 3, 4, 5]

        nums[4] = 8;
        System.out.println(Arrays.toString(nums));    //[0, 1, 2, 3, 8, 5]
        System.out.println(Arrays.toString(cnums));  //[0, 1, 2, 3, 4, 5]


        People p1 = new People("a", 0);
        People p2 = new People("b", 1);
        People p3 = new People("c", 2);
        People[] ps = {p1, p2, p3};
        People[] cps = ps.clone();
        print(ps);    //People{name='a', age=0} People{name='b', age=1} People{name='c', age=2}
        print(cps);   //People{name='a', age=0} People{name='b', age=1} People{name='c', age=2}
        
        p1.setAge(100);
        print(ps);    //People{name='a', age=100} People{name='b', age=1} People{name='c', age=2}
        print(cps);   //People{name='a', age=100} People{name='b', age=1} People{name='c', age=2}



    }

    static void print(Object[] objects) {
        for (Object obj : objects) {
            System.out.print(obj +  " ");
        }
        System.out.println();
    }
}


class People{

    private String name;
    private int age;

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    protected void say(){
        System.out.println("!!!");
    }
}

7. toString() 方法不做什麼介紹

8. notify() 方法

public final native void notify();

  a. 關鍵字 final

  b. 關鍵字 native

  c. 喚醒一個正在等待該資源的執行緒:

    1) 喚醒哪個執行緒是不確定的,任意的

    2) 呼叫該方法的前提是當前執行緒必須持有該物件的 monitor, 否則丟擲 IllegalMonitorStateException

9. notifyAll() 方法

public final native void notifyAll();

  a. 關鍵字 final

  b. 關鍵字 native

  c. 喚醒所有正在等待該資源的執行緒:,讓他們去競爭這個資源, 呼叫該方法的前提是當前執行緒必須持有該物件的 monitor, 否則丟擲 IllegalMonitorStateException

10. wait() 方法: wait() 方法是和 notify() 配合使用的