java,lang.Void类

来源:互联网 发布:关于网络文化的数据 编辑:程序博客网 时间:2024/06/05 15:12

在一次源码查看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类可能本身作用就只是不起任何作用,但是本身只是一个占位符类。

0 0
原创粉丝点击