首頁  >  文章  >  Java  >  java.lang.Void類別源碼的詳細介紹

java.lang.Void類別源碼的詳細介紹

黄舟
黄舟原創
2017-10-11 10:01:091417瀏覽

這篇文章主要介紹了java.lang.Void類別原始碼解析的相關內容,對原始碼中的部分內容進行解釋,具有一定參考價值,需要的朋友可以了解下。

 在一次原始碼查看ThreadGroup的時候,看到一段程式碼,為以下程式碼:


/*
   * @throws NullPointerException if the parent argument is {@code null}
   * @throws SecurityException   if the current thread cannot create a
   *                thread in the specified thread group.
   */
  private static Void checkParentAccess(ThreadGroup parent) {
    parent.checkAccess();
    return null;
  }

            這個方法用於檢查parent存取權限,然後直接回傳null,方法的回傳型別為Void原以為Void類為void類的包裝類,但查看Void類的

源碼後發現並不是如此,Void類的源碼如下:


/**
 * The {@code Void} class is an uninstantiable placeholder class to hold a
 * reference to the {@code Class} object representing the Java keyword
 * void.
 *
 * @author unascribed
 * @since  JDK1.1
 */
public final
class Void {
  /**
   * The {@code Class} object representing the pseudo-type corresponding to
   * the keyword {@code void}.
   */
  @SuppressWarnings("unchecked")
  public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");
  /*
   * The Void class cannot be instantiated.
   */
  private Void() {}
}

在最上面的註解中,描述的是


##

The {@code Void} class is an uninstantiable placeholder class to hold a
* reference to the {@code Class} object representing the Java keyword

這段話的意思是Void類別是一個不可實例化的佔位符類,它持有對標識Java關鍵字void的Class物件的參考。


且本身的建構子為private,並且註明:



#

public final class Void {}

final表示這個類別是不允許被其他類繼承的。


/*
 * The Void class cannot be instantiated.
 */

即該類別是不可以實例化的。

Void類別可能本身作用就只是不起任何作用,但本身只是佔位符類別。即Void類別本身只是佔位符類,不能被實例化,多用於泛型中作佔位符使用。

總結

以上是java.lang.Void類別源碼的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn