Java编程:java.lang.Void类分析

源码

/*
 * 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类本身只是一个占位符类,不能被实例化,多用于泛型中作占位符使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值