Q:

Java program to check whether the specified Class object represents a primitive type or not

belongs to collection: Java Class and Object Programs

0

Java program to check whether the specified Class object represents a primitive type or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will check whether an object represents a primitive type or not using the isPrimitive() method.

Program/Source Code:

The source code to check whether the specified Class object represents a primitive type or not is given below. The given program is compiled and executed successfully.

// Java program to check the specified Class object 
// represents a primitive type

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls1 = int.class;
    Class cls2 = Class.forName("Main");

    boolean res1 = cls1.isPrimitive();
    boolean res2 = cls2.isPrimitive();

    System.out.println("Is int a primitive type : " + res1);
    System.out.println("Is Main a primitive type : " + res2);
  }
}

Output:

Is int a primitive type : true
Is Main a primitive type : false

Explanation:

In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. Here, we checked whether an object represents a primitive type or not using the isPrimitive() method. The isPrimitive() method returns true if the specified object represents a primitive type otherwise it returns false.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check whether the specified Class ... >>
<< Java program to check whether the specified Class ...