java.lang.Void類(lèi)源碼解析
在一次源碼查看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訪問(wèn)權(quán)限,然后直接返回null,方法的返回類(lèi)型為Void原以為Void類(lèi)為void類(lèi)的包裝類(lèi),但是查看Void類(lèi)的
源碼后發(fā)現(xiàn)并不是如此,Void類(lèi)的源碼如下:
/** * 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類(lèi)是一個(gè)不可實(shí)例化的占位符類(lèi),它持有對(duì)標(biāo)識(shí)Java關(guān)鍵字void的Class對(duì)象的引用。
并且本身的構(gòu)造函數(shù)為private,并且注明:
public final class Void {}
final表明這個(gè)類(lèi)是不允許被其他類(lèi)繼承的。
/* * The Void class cannot be instantiated. */
即該類(lèi)是不可以實(shí)例化的。
Void類(lèi)可能本身作用就只是不起任何作用,但是本身只是一個(gè)占位符類(lèi)。即Void類(lèi)本身只是一個(gè)占位符類(lèi),不能被實(shí)例化,多用于泛型中作占位符使用。
總結(jié)
以上就是本文關(guān)于java.lang.Void類(lèi)源碼解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:RateLimiter 源碼分析、基于ZooKeeper實(shí)現(xiàn)隊(duì)列源碼、Spring SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析等,有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。
相關(guān)文章
Spring boot如何基于攔截器實(shí)現(xiàn)訪問(wèn)權(quán)限限制
這篇文章主要介紹了Spring boot如何基于攔截器實(shí)現(xiàn)訪問(wèn)權(quán)限限制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java單表實(shí)現(xiàn)評(píng)論回復(fù)功能(多種實(shí)現(xiàn)方式)
這篇文章主要介紹了Java單表實(shí)現(xiàn)評(píng)論回復(fù)功能,大家都知道評(píng)論功能有多種實(shí)現(xiàn)方式,本文逐一給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Unicode、UTF-8 和 ISO8859-1區(qū)別解析
這篇文章主要介紹了Unicode、UTF-8 和 ISO8859-1到底有什么區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01使用maven方式創(chuàng)建springboot項(xiàng)目的方式
使用Spring Initializr創(chuàng)建spring boot項(xiàng)目,因?yàn)橥饩W(wǎng)問(wèn)題導(dǎo)致很難成功,所以只能使用maven方式,這里介紹下使用maven方式創(chuàng)建springboot項(xiàng)目的方法,感興趣的朋友一起看看吧2022-09-09SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03Java的MyBatis框架項(xiàng)目搭建與hellow world示例
MyBatis框架為Java程序的數(shù)據(jù)庫(kù)操作帶來(lái)了很大的便利,這里我們就從最基礎(chǔ)的入手,來(lái)看一下Java的MyBatis框架項(xiàng)目搭建與hellow world示例,需要的朋友可以參考下2016-06-06