Java uses class objects to represent all eight primitive types. A class object that represents a primitive type can be identified using the isPrimitive()
method call. void
is not a type in Java, but the isPrimitive()
method returns true
for void.class
.
package org.kodejava.lang.reflect;
public class IsPrimitiveDemo {
public static void main(String[] args) {
IsPrimitiveDemo.get(int.class);
IsPrimitiveDemo.get(String.class);
IsPrimitiveDemo.get(double.class);
IsPrimitiveDemo.get(void.class);
}
private static void get(Class<?> clazz) {
if (clazz.isPrimitive()) {
System.out.println(clazz.getName() +
" is a primitive type.");
} else {
System.out.println(clazz.getName() +
" is not a primitive type.");
}
}
}
Here is the result of the program:
int is a primitive type.
java.lang.String is not a primitive type.
double is a primitive type.
void is a primitive type.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024