Java

How to Get Type of Object in Java?

An object is a physical entity that has its own state and behavior, and it acts as a unique instance of a Java class. It. When the object originates from a source, it is considered useful to examine the object type. Also, knowing the type of an object is crucial when working with a collection that includes different objects or when it is required to execute the logical operation with the same kind of variables.

This article will help you to learn the method to get the type of an object in Java.

How to Get Type of Object in Java?

For getting the type of predefined or user-defined class object in Java, you can use:

  • getClass() method
  • instanceof operator

We will now check out each of the mentioned methods one by one!

Method 1: Get Type of Pre-defined Class Object Using getClass() Method

In Java, we have predefined classes like wrapper classes such as String, Double, Integer, and many more. Sometimes we need to verify the object type while using predefined classes. For this purpose, Java offers a “getClass()” method that belongs to the “Object” class.

Syntax
The syntax of the “getClass()” method is given as follows:

x.getClass()

Here, the “getClass()” method will return the class of the specified “x” object.

Example
In this example, we will create a String type object named “x” containing the following value:

String x = "Hello";

Next, we will print a statement using the “System.out.println()” method:

System.out.println("x belongs to? ");

Lastly, we will get the type of the object “x” by calling the “getClass()” method:

System.out.print(x.getClass());

The output shows that the created variable belongs to the Java String class:

Let’s see another method to get the object type using the “instanceof” operator.

Method 2: Get Type of Pre-defined Class Object Using “instanceof” Operator

You can also utilize the “instanceof” operator to check the object type in a Java program. This operator returns a boolean value which indicates whether the object is an instance of the particular class or not.

Syntax
The syntax of the “instanceof” is as follows:

x instanceof Integer

Here, “x” is an object and “Integer” is the predefined Java wrapper class. The “instanceof” operator checks whether the object belongs to the mentioned class or not and returns a boolean value.

Example
In this example, we have an object “x” of the Integer class having “5” as its value:

Integer x = 5;

Next, we will print a statement using the “System.out.println()” method:

System.out.print("x is an instance of Integer class? ");

Now, we will check whether the object is an instance of an Integer class or not:

System.out.print(x instanceof Integer);

The output displayed “true” as the object “x” is an instance of the Integer class:

At this point, you may be wondering about how to get the type of user-defined class object. The below-given section will assist you in this regard.

Method 3: Get Type of User-defined Class Object Using getClass() Method

You can also get the type of the user-defined class object with the help of the “getClass()” method. In such a scenario, we will compare the object with the class name using the “==” comparison operator.

Syntax
For the specified purpose, the syntax of “getClass()” method is given as:

myclassObj.getClass() == MyClass.class

Here, the “getClass()” method is called by the “myclassObj” object of the “MyClass” and then compared with the name using the comparison operator “==”.

Example
In this example, we have three classes named “MyClass”, “MynewClass”, and “Example”, where MyClass acts as a parent class of MynewClass:

class MyClass{}

The “MynewClass” is a child class as it is extended from “MyClass”:

class MynewClass extends MyClass{}

In the main() method of the class “Example”, we will declare and instantiate an object of the parent class “MyClass”. Then check whether the created object belongs to which class; parent or child? To do so, we will call the “getClass()” method with the created object and compare the resultant value with parent and child class names using if-else-if conditions:

public class Example {
    public static void main(String[] args) {
        MyClass myclassObj = new MyClass();
         if (myclassObj.getClass() == MyClass.class) {
                System.out.println("The object 'myclassObj' is a type of 'MyClass'");
            } else if (myclassObj.getClass() == MynewClass.class) {
                System.out.println("The object 'myclassObj' is a type of 'MynewClass'");
            }  
    }
}

The output indicates that the object “myclassObj” belongs to the parent class named “MyClass”:

Now, head toward the next section!

Method 4: Get Type of User-defined Class Object Using “instanceof” Operator

Similar to predefined classes, for user-defined classes, you can also get the type of object by using the “instanceof” operator.

Syntax
The syntax is given below:

myclassObj instanceof MyClass

Here, the “instanceof” operator will check if the “myclassObj” is an instance of “MyClass” or not.

Example
We will now utilize the same classes which we have created in the previously mentioned example. The only difference is that we will use the “instanceof” operator to verify if the created object instance belongs to the parent or child class:

public class Example {
    public static void main(String[] args) {
        MyClass myclassObj = new MyClass();
         if (myclassObj instanceof MyClass) {
                System.out.println("The object 'myclassObj' is an instance of 'MyClass'");
            } else if (myclassObj instanceof MynewClass) {
                System.out.println("The object 'myclassObj' is an instance of 'MynewClass'");
            }  
    }
}

The given output shows that the “instanceof” operator validated the type of the object as “MyClass”:

We have compiled all methods related to getting object type in Java.

Conclusion

To get a type of object in Java, you can use the “getClass()” method or the “instanceof” operator. These methods can be used to check object types for both predefined and user-defined classes. The getClass() method returns the class name while the “instanceof” operator returns a boolean value, where “true” indicates the object belongs to that specified class; otherwise, it returns “false”. This article provided all the methods to get the object type in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.