What Is java.lang.Class Class?

This section describes what is java.lang.Class class - A built-in class that represent instances of all data types used in a Java application in the JVM.

What Is java.lang.Class Class? java.lang.Class is a built-in class that represent instances of all data types used in a Java application in the JVM. Instances of classes, interfaces, enumerations, annotations, arrays, primitives and void are all represented Class objects.

To access the Class object of a data type, you can use the class literal - the name of the name of a class, interface, array, or primitive type followed by a `.' and the token class. For example:

Class c = String.class;
Class c = Iterable.class;
Class c = byte[].class;
Class c = int.class;
Class c = void.class;

The Class class offers a set of methods to help to determine if the given Class object represents a specific group of data type:

Here is tutorial example program showing you how to use the class literal and these isXXX() methods:

 * ClassDataType.java
 * Copyright (c) 2010, HerongYang.com, All Rights Reserved.
class ClassDataType {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Class c = null;

      // Using class literals
      c = String.class;
      out.println(c.getName()+" isLocalClass: "+c.isLocalClass());
      c = java.util.Iterator.class;
      out.println(c.getName()+" isInterface: "+c.isInterface());

      c = Thread.State.class;
      out.println(c.getName()+" isEnum: "+c.isEnum());

      c = Override.class;
      out.println(c.getName()+" isAnnotation: "+c.isAnnotation());

      c = byte[].class;
      out.println(c.getName()+" isArray: "+c.isArray());

      c = String[].class;
      out.println(c.getName()+" isArray: "+c.isArray());

      c = int.class;
      out.println(c.getName()+" isPrimitive: "+c.isPrimitive());

      c = void.class;
      out.println(c.getName()+" isPrimitive: "+c.isPrimitive());

When executed on my Windows 7 system with JDK 1.7.0, I got this result:

C:\herong\jvm>java ClassDataType
java.lang.String isLocalClass: false
java.util.Iterator isInterface: true
java.lang.Thread$State isEnum: true
java.lang.Override isAnnotation: true
[B isArray: true
[Ljava.lang.String; isArray: true
int isPrimitive: true
void isPrimitive: true

The test result tells me that:

When tested with program with JDK 1.6.0 in 2010, I got the same results.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

Class Class - Class Reflections

What Is java.lang.Class Class?

 forName() Method - Loading Classes

 Class Reflection and Introspection

 Array Class Introspection

 Invoking Methods of Class Instances

 Sun's JVM - Java HotSpot VM

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 Outdated Tutorials


 PDF Printing Version