Java program to determine the class of an object

www.i‮aeditfig‬.com

To determine the class of an object in Java, you can use the getClass() method provided by the Object class. Here is an example program that demonstrates how to use the getClass() method:

public class ObjectClassExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        Integer num = 10;
        Double dbl = 3.14;
        
        System.out.println("Class of str: " + str.getClass().getName());
        System.out.println("Class of num: " + num.getClass().getName());
        System.out.println("Class of dbl: " + dbl.getClass().getName());
    }
}

In this program, we create three objects of different types - a String, an Integer, and a Double. We then use the getClass() method to determine the class of each object. The getName() method is called on the result of the getClass() method to get the fully qualified name of the class.

When you run this program, you will get output like this:

Class of str: java.lang.String
Class of num: java.lang.Integer
Class of dbl: java.lang.Double

This shows that the getClass() method returns a Class object that represents the class of the object. The getName() method returns a String that represents the fully qualified name of the class.