JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
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 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) HerongYang.com. All Rights Reserved. */ class ClassDataType { public static void main(String[] a) { java.io.PrintStream out = System.out; Class c = null; 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, I got this result:
herong> 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:
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
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
Invoking Methods of Class Instances
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
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