Table of contents
1.
Introduction
2.
How to Use typeof in Java?
3.
Approach 1: Return the Type Using the "getClass()" Method
4.
Approach 2: Return the Type Using the "instanceof" Operator
5.
Approach 3: Return the Type Using the "isInstance()" Method
6.
Frequently Asked Questions
6.1.
Can you use typeof to check if an object is null?
6.2.
Is it possible to use typeof with primitive types in Java?
6.3.
Can typeof be used to check if an object is an instance of an interface?
7.
Conclusion
Last Updated: Nov 10, 2024
Easy

How to Use typeof in Java?

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the typeof operator is used to determine the type of a variable or expression at runtime. It helps you to check whether an object is an instance of a specific class or interface. This can be useful in different situations, like when we are working with polymorphism or when we need to perform different actions based on the type of an object. 

How to Use typeof in Java

In this article, we'll discuss how to use the typeof operator in Java and look at different approaches to determine the type of an object.

How to Use typeof in Java?

In Java, there are a few different ways to determine the type of an object using the typeof operator. We'll understand all three common approaches mentioned below :

1. Using the getClass() method
 

2. Using the instanceof operator 
 

3. Using the isInstance() method
 

Each of these methods have their different use case and advantages. Now, we will discuss these methods in detail with examples.

Approach 1: Return the Type Using the "getClass()" Method

The getClass() method is a built-in method in Java that returns the runtime class of an object. It is defined in the Object class, which means it is available for all objects in Java. 

Lets see how you can use the getClass() method to determine the type of an object:

public class Main {
    public static void main(String[] args) {
        Object obj = "Hello";
        Class<?> type = obj.getClass();
        System.out.println(type.getSimpleName());
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

String


In this example, we have an object obj of type String. By calling the getClass() method on obj, we get the Class object representing the runtime class of the object. We can then use methods like getSimpleName() or getName() on the Class object to retrieve the name of the class.
 

When to use: The getClass() method is useful when you have a reference to an object and want to determine its exact runtime type. It provides a easy way to obtain the Class object, which you can then use to inspect various properties of the class.

Approach 2: Return the Type Using the "instanceof" Operator

The instanceof operator is another way to check the type of an object in Java. It tests whether an object is an instance of a specific class or interface. The instanceof operator returns a boolean value indicating whether the object is of the specified type or not.

For example : 

public class Main {
    public static void main(String[] args) {
        Object obj = "Hello";
        if (obj instanceof String) {
            System.out.println("The object is an instance of String.");
        } else {
            System.out.println("The object is not an instance of String.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

The object is an instance of String.


In this example, we have an object obj of type String. We use the instanceof operator to check if obj is an instance of the String class. Since it is indeed a String object, the condition evaluates to true & the corresponding message is printed.

 

When to use : The instanceof operator is very useful when you have a reference to an object and want to perform different actions based on its type. You can use instanceof to test against multiple classes or interfaces and execute specific code blocks accordingly.
 

Note: One important point to remember is that the instanceof operator also considers inheritance. If an object is an instance of a subclass, it is also considered an instance of its superclass. This allows you to write more generic code that can handle objects of different subclasses.

Approach 3: Return the Type Using the "isInstance()" Method

The isInstance() method is a method of the Class class in Java. It allows you to check whether an object is an instance of a particular class or interface. The isInstance() method takes an object as a parameter & returns a boolean value indicating whether the object is an instance of the class represented by the Class object.


For example : 

public class Main {
    public static void main(String[] args) {
        Object obj = "Hello";
        Class<String> stringClass = String.class;
        if (stringClass.isInstance(obj)) {
            System.out.println("The object is an instance of String.");
        } else {
            System.out.println("The object is not an instance of String.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

The object is an instance of String.


In this example, we have an object obj of type String & a Class object stringClass representing the String class. We use the isInstance() method of stringClass to check if obj is an instance of String. Since it is indeed a String object, the condition evaluates to true & the corresponding message is printed.
 

The isInstance() method solves the same purpose to the instanceof operator but it provides a more dynamic way to check the type of an object. Instead of hardcoding the class name in the code, you can obtain the Class object dynamically and use it with the isInstance() method.
 

When to use : This approach can be useful when you have a Class object representing a particular class and want to check if an object belongs to that class. It allows for more flexible and dynamic type checking.

Frequently Asked Questions

Can you use typeof to check if an object is null?

No, typeof cannot be used to check for null. Instead, you should use the equality operator (==) to compare an object with null.

Is it possible to use typeof with primitive types in Java?

No, typeof is used with object references, not primitive types. For primitives, you can use the instanceof operator with their wrapper classes.

Can typeof be used to check if an object is an instance of an interface?

Yes, typeof can be used to check if an object implements a specific interface using the instanceof operator or the isInstance() method.

Conclusion

In this article, we have learned about the typeof operator in Java and discussed all the three approaches to determine the type of an object: using the getClass() method, the instanceof operator, & the isInstance() method. Each approach has its own use case and advantages, which allows you to check the type of an object in different situations. 

You can also check out our other blogs on Code360.

Live masterclass