How to determine an object's class?

To determine the class of an object in Java, you can use the getClass() method of the Object class. The getClass() method returns a Class object that represents the object's class.

Here's an example of how you can use the getClass() method to determine an object's class:

public class Main {
  public static void main(String[] args) {
    Object obj = "hello";
    Class c = obj.getClass();
    System.out.println(c.getName()); // prints "java.lang.String"
  }
}

In this example, the obj variable is an Object that refers to a String object. The getClass() method is called on the obj object to get a Class object that represents the object's class. The getName() method of the Class object is then called to get the name of the class, which is printed to the console.

You can also use the instanceof operator to check if an object is an instance of a particular class or interface. For example:

public class Main {
  public static void main(String[] args) {
    Object obj = "hello";
    if (obj instanceof String) {
      System.out.println("obj is an instance of String");
    }
  }
}

I hope this helps! Let me know if you have any questions.