Java 原始碼賞析 - java.lang - Void

Richaaaard發表於2014-03-15

被人鄙視了,於是也來讀讀原始碼。。。

 

package java.lang;

/**
 * The Void class is an uninstantiable placeholder class to hold a
 * reference to the Class object representing the Java keyword
 * void.
 *
 * @author  unascribed
 * @version %I%, %G%
 * @since   JDK1.1
 */
public final
class Void {

    /**
     * The Class object representing the pseudo-type corresponding to
     * the keyword void.
     */
    public static final Class<Void> TYPE = Class.getPrimitiveClass("void");

    /*
     * The Void class cannot be instantiated.
     */
    private Void() {}
}

原來Java裡面有個Void類,是一個不可例項化的佔位(placeholder)類,它持有一個Void型別的類變數來表示Java裡面的關鍵字void。

Class.getPrimitiveClass() 原來還有這個方法,在什麼場景下使用?

是不是所有的關鍵字都有對應的一個類呢?

這些關鍵字在編譯好的檔案中是怎樣表示的?

Java直譯器又是怎樣工作的?

相關文章