Home »
Java programming language
Java Class class isPrimitive() method with example
Class class isPrimitive() method: Here, we are going to learn about the isPrimitive() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 02, 2019
Class class isPrimitive() method
- isPrimitive() method is available in java.lang package.
- isPrimitive() method is used to check whether this Class object denotes a primitive type or not.
- In Java, we have a predefined Class object to denote primitive and void but the important thing Class object has a similar name as primitives like byte, char, short, int, long, float and double.
- isPrimitive() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- isPrimitive() method does not throw an exception at the time of checking primitive.
Syntax:
public boolean isPrimitive();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is boolean, it returns a boolean value based on the following cases,
- It returns true, when this Class object denotes primitive type.
- It returns false, when this Class object does not denote a primitive type.
Example:
// Java program to demonstrate the example
// of boolean isPrimitive() method of Class
public class IsPrimitiveOfClass {
public static void main(String[] args) {
// Create and Return String class
String str = new String();
Class cl1 = str.getClass();
// Create and Return short
short sh = 10;
Class cl2 = short.class;
// We are checking the class denotes primitive type
boolean b1 = cl1.isPrimitive();
boolean b2 = cl2.isPrimitive();
System.out.print("Is" + " " + cl1.getSimpleName() + " ");
System.out.println("Primitive" + ": " + b1);
System.out.print("Is" + " " + cl2.getSimpleName() + " ");
System.out.println("Primitive" + ": " + b2);
}
}
Output
Is String Primitive: false
Is short Primitive: true