这篇文章主要介绍了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类源码的详细介绍的详细内容。
