java.lang.Void类源码解析

作者:叶长风 时间:2022-07-15 04:07:46 

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


/*
  * @throws NullPointerException if the parent argument is {@code null}
  * @throws SecurityException   if the current thread cannot create a
  *                thread in the specified thread group.
  */
 private static Void checkParentAccess(ThreadGroup parent) {
   parent.checkAccess();
   return null;
 }

            这个方法用于检查parent访问权限,然后直接返回null,方法的返回类型为Void原以为Void类为void类的包装类,但是查看Void类的

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


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

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


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


public final class Void {}

final表明这个类是不允许被其他类继承的。


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

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

Void类可能本身作用就只是不起任何作用,但是本身只是一个占位符类。即Void类本身只是一个占位符类,不能被实例化,多用于泛型中作占位符使用。

来源:http://blog.csdn.net/u012734441/article/details/51614214

标签:java,源码
0
投稿

猜你喜欢

  • springmvc不进入Controller导致404的问题

    2023-03-18 01:29:20
  • Spring使用三级缓存解决循环依赖的问题

    2023-03-14 09:06:15
  • Spring JPA find单表查询方法示例详解

    2022-11-03 19:43:38
  • java多线程中的异常处理机制简析

    2021-11-18 01:54:39
  • SpringBoot Security安装配置及Thymeleaf整合

    2023-11-27 16:18:41
  • Java编程使用卡片布局管理器示例【基于swing组件】

    2022-03-03 10:46:33
  • Java实现插入公式到PPT的示例代码

    2023-11-12 03:04:41
  • Java DecimalFormat 保留小数位及四舍五入的陷阱介绍

    2023-11-09 04:49:33
  • Winform 显示Gif图片的实例代码

    2021-06-29 21:14:44
  • opencv利用鼠标滑动画出多彩的形状

    2023-11-03 05:20:57
  • SpringBoot之Controller的使用详解

    2023-02-16 21:18:30
  • Java循环对bean的属性进行赋值的实现

    2023-01-27 10:18:05
  • Java中类的加载顺序执行结果

    2022-03-12 18:12:35
  • Java请求转发和请求重定向区别详解

    2023-05-19 07:30:17
  • 一文带你了解SpringBoot的启动原理

    2023-11-28 20:44:42
  • springMvc注解之@ResponseBody和@RequestBody详解

    2022-10-09 17:57:19
  • Java中stream处理中map与flatMap的比较和使用案例

    2023-11-21 02:27:53
  • spring boot 2整合swagger-ui过程解析

    2021-08-08 22:57:35
  • Java SpringSecurity+JWT实现登录认证

    2022-12-13 16:44:18
  • JDK1.8下载、安装和环境配置超详细教程(最新最完整)

    2022-07-22 12:58:34
  • asp之家 软件编程 m.aspxhome.com