java.lang.Void类源码解析-成都快上网建站

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 TYPE = (Class) 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类源码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:RateLimiter 源码分析、基于ZooKeeper实现队列源码、Spring SpringMVC在启动完成后执行方法源码解析等,有什么问题可以随时留言,小编会及时回复大家的。


名称栏目:java.lang.Void类源码解析
文章链接:http://kswjz.com/article/gsegeg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流