Home  >  Article  >  Java  >  Detailed introduction of java.lang.Void class source code

Detailed introduction of java.lang.Void class source code

黄舟
黄舟Original
2017-10-11 10:01:091481browse

This article mainly introduces the relevant content of java.lang.Void class source code analysis, and explains some contents in the source code. It has certain reference value. Friends who need it can learn more.

When I checked the source code of ThreadGroup, I saw a piece of code, which is as follows:


/*
   * @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;
  }

This method is used to check parent access permissions. Then it returns null directly. The return type of the method is Void. I originally thought that the Void class is a wrapper class of the void class, but after checking the

source code of the Void class, I found that this is not the case. The source code of the Void class is as follows:


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

In the top comment, it describes the passage


The {@code Void} class is an uninstantiable placeholder class to hold a
* reference to the {@code Class} object representing the Java keyword

What it means is that the Void class is a non-instantiable placeholder class that holds a reference to the Class object that identifies the Java keyword void.

And its own constructor is private, and noted:


public final class Void {}

final indicates that this class is not allowed to be used by other classes inherited.


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

That is, this class cannot be instantiated.

The Void class may not have any effect, but it is just a placeholder class. That is, the Void class itself is just a placeholder class and cannot be instantiated. It is mostly used as a placeholder in generics.

Summarize

The above is the detailed content of Detailed introduction of java.lang.Void class source code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn