java,lang.Void类

本文解析了Java中ThreadGroup的一个内部方法checkParentAccess,该方法用于验证父线程组的访问权限,并探讨了其返回类型Void的实际意义及Void类的作用。

在一次源码查看ThreadGroup的时候,看到一段代码,为以下:

   

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.      * @throws  NullPointerException  if the parent argument is {@code null} 
  3.      * @throws  SecurityException     if the current thread cannot create a 
  4.      *                                thread in the specified thread group. 
  5.      */  
  6.     private static Void checkParentAccess(ThreadGroup parent) {  
  7.         parent.checkAccess();  
  8.         return null;  
  9.     }  
            这个方法用于检查parent访问权限,然后直接返回null,方法的返回类型为 Void 原以为Void类为void类的包装类,但是查看Void类的

源码后发现并不是如此,Void类的源码如下:

           

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * The {@code Void} class is an uninstantiable placeholder class to hold a 
  3.  * reference to the {@code Class} object representing the Java keyword 
  4.  * void. 
  5.  * 
  6.  * @author  unascribed 
  7.  * @since   JDK1.1 
  8.  */  
  9. public final  
  10. class Void {  
  11.   
  12.     /** 
  13.      * The {@code Class} object representing the pseudo-type corresponding to 
  14.      * the keyword {@code void}. 
  15.      */  
  16.     @SuppressWarnings("unchecked")  
  17.     public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");  
  18.   
  19.     /* 
  20.      * The Void class cannot be instantiated. 
  21.      */  
  22.     private Void() {}  
  23. }  
                

            在最上面的注释中,描述的是

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,并且注明:

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

即该类是不可以实例化的。

        Void类可能本身作用就只是不起任何作用,但是本身只是一个占位符类。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值