java.lang.Void类源码解析_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习

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

总结

以上就是本文关于java.lang.Void类源码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:RateLimiter 源码分析、基于ZooKeeper实现队列源码、Spring SpringMVC在启动完成后执行方法源码解析等,有什么问题可以随时留言,小编会及时回复大家的。

原文地址是:http://www.piaodoo.com/thread-13240-1-2.html 丝袜控www.txdah.com 131www.buzc.org学习之外可赏心悦目有助更好地学习!

原文地址:https://www.cnblogs.com/txdah/p/12093940.html

时间: 2024-07-29 08:47:07

java.lang.Void类源码解析_java - JAVA的相关文章

java.lang.Boolean 类源码解析

Boolean源码比较简单. 1 public final class Boolean implements java.io.Serializable, 2 Comparable<Boolean> 3 { 4 /** 5 * The {@code Boolean} object corresponding to the primitive 6 * value {@code true}. 7 */ 8 public static final Boolean TRUE = new Boolean(

Java集合---Array类源码解析

Java集合---Array类源码解析              ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在

java.lang.String 类源码解读

String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public final class String defined String由char[]数组实现 /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the strin

Java集合---Arrays类源码解析

一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在<<The Java Tutorial>>中做出的解释如下: The sort

java.lang.Long 类源码解读

总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. 1 // final修饰符 2 public final class Long extends Number implements Comparable<Long> { 3 /** 4 * A constant holding the minimum value a {@code long} can 5 * have, -2<sup>63</sup>. 6 */ 7 // 最

java.lang.Byte 类源码浅析

Byte 类字节,属于Number. 1 public final class Byte extends Number implements Comparable<Byte> { 2 3 /** 4 * A constant holding the minimum value a {@code byte} can 5 * have, -2<sup>7</sup>. 6 */ 7 public static final byte MIN_VALUE = -128; 8 9

There is no getter for property named &#39;*&#39; in &#39;class java.lang.String&#39;之源码分析

There is no getter for property named '*' in 'class java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用<when test="username != null">这样的条件判断时,就会出现该错误,不过今天我们来刨根问底一下. 一.错误再现 想要追本溯源,就需要错误再现,那么假设我们有这样一个sql查询

java-AbstractCollection类-源码解析

转载:原文地址 http://www.cnblogs.com/android-blogs/p/5566212.html 一.Collection接口 从<Java集合:整体结构>一文中我们知道所有的List和Set都继承自Collection接口,该接口类提供了集合最基本的方法,虽然List接口和Set等都有一些自己独有的方法,但是基本的操作类似.我们先看下Collection接口提供的方法: 总体上可以将Collection的方法分为以下几大类: 1.增加(add/addAll) 2.删除(

Java集合类库 ArrayList 源码解析

集合类库是Java的一个重大突破,方便了我们对大数据的操作.其中 Arrays 和 Collections 工具类可以帮助我们快速操作集合类库.下面对Java集合类库的源码分析是基于jdk1.7的.今天我们来看看ArrayList的底层实现原理. ArrayList的继承结构图 继承自 AbstractList 抽象类,在上层是 AbstractCollection 抽象类,直接去 AbstractCollection 类去看看. AbstractCollection 类主要实现了 Collec