java.lang.Void類源碼解析
在一次源碼查看ThreadGroup的時(shí)候,看到一段代碼,為以下:
/*
* @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;
}
這個(gè)方法用于檢查parent訪問權(quán)限,然后直接返回null,方法的返回類型為Void原以為Void類為void類的包裝類,但是查看Void類的
源碼后發(fā)現(xiàn)并不是如此,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類是一個(gè)不可實(shí)例化的占位符類,它持有對(duì)標(biāo)識(shí)Java關(guān)鍵字void的Class對(duì)象的引用。
并且本身的構(gòu)造函數(shù)為private,并且注明:
public final class Void {}
final表明這個(gè)類是不允許被其他類繼承的。
/* * The Void class cannot be instantiated. */
即該類是不可以實(shí)例化的。
Void類可能本身作用就只是不起任何作用,但是本身只是一個(gè)占位符類。即Void類本身只是一個(gè)占位符類,不能被實(shí)例化,多用于泛型中作占位符使用。
總結(jié)
以上就是本文關(guān)于java.lang.Void類源碼解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:RateLimiter 源碼分析、基于ZooKeeper實(shí)現(xiàn)隊(duì)列源碼、Spring SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。
相關(guān)文章
通過idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動(dòng)過程圖解
這篇文章主要介紹了通過idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動(dòng)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
java實(shí)現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時(shí)素材
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時(shí)素材,網(wǎng)絡(luò)圖片上傳到微信服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
mybatis的foreach標(biāo)簽語法報(bào)錯(cuò)的解決
這篇文章主要介紹了mybatis的foreach標(biāo)簽語法報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
RabbitMQ實(shí)現(xiàn)Work Queue工作隊(duì)列的示例詳解
工作隊(duì)列(又稱任務(wù)隊(duì)列)的主要思想是避免立即執(zhí)行資源密集型任務(wù),而不得不等待它完成。本篇文章將記錄和分享RabbitMQ工作隊(duì)列相關(guān)的知識(shí)點(diǎn),希望對(duì)大家有所幫助2023-01-01
SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn)
本文主要介紹了SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

