源码看JAVA【十二】Void

1、源码查看

 /**
 * 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() {}
}

2、Void与void

Void是void的封装类

3、Void作用

在方法返回值反射时可以使用Void.TYPE来判断类型。也可以使用void.class来判断。

4、总结

实际作用并不大,从声明来看,有用部分也只声明了TYPE。


   转载规则


《源码看JAVA【十二】Void》 Euler 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录