源码
/*
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
* 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}.
*/
public static final Class<Void> TYPE = 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.
Void类是一个不可实例化的占位符类,它持有一个 代表Java关键字void
的Class对象的引用。
The {@code Class} object representing the pseudo-type corresponding to
* the keyword {@code void}.
这个一个与关键字void一致的伪类型对象。
代码分析
public final class Void {}
final表明这个类是不允许被其他类继承的。
public static final Class<Void> TYPE = Class.getPrimitiveClass("void");
获取void
类型的字节码,将其引用赋值给Void
类的TYPE属性。表名这是一个void关键字的Class对象。
8种基础数据类型也都有对应的TYPE属性,我们看一下Integer类中TYPE属性的定义:
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since JDK1.1
*/
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
类实例代表原始类型int
,我的理解是将Class<Integer>
类实例化后就代表一个int类型。static、final修饰。
private Void() {}
构造函数使用private修饰符,表明这个类不能被实例化。
Void的作用
我的分析:Void类本身只是一个占位符类,不能被实例化,多用于泛型中作占位符使用。