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:
boolean isAnnotation() - Returns true if this Class object represents an annotation type.
boolean isArray() - Determines if this Class object represents an array class.
boolean isEnum() - Returns true if and only if this class was declared as an enum in the source code.
boolean isInterface() - Determines if the specified Class object represents an interface type.
boolean isPrimitive() - Determines if the specified Class object represents a primitive type.
Here is tutorial example program showing you how to use the class literal and these isXXX() methods:
/**
* ClassDataType.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
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 XP system with JDK 1.6.0,
I got this result: