All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Check Type Of Variable In Java

Last Updated : Mar 11, 2024

How To Check Type Of Variable In Java

In this article we will show you the solution of how to check type of variable in java, using Java's "instanceof" operator, you can determine a variable's type. Indicating if an object is an instance of the provided class or one of its subclasses, this operation returns a boolean value.

Simply write the variable, the keyword "instanceof," and the class name to utilise the "instanceof" operator.

For instance, you would type "obj instanceof String" if you had the variable "obj" and wanted to see if it was an instance of the String class.

The result will return true if "obj" is a String; otherwise, it's going to return false. We will now discuss the idea of checking a variable's type in Java.

Step By Step Guide On How To Check Type Of Variable In Java :-

public class VariableTypeChecker {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int num = 42;
        double decimal = 3.14;
        boolean bool = true;
        char character = 'A';
    public static void checkVariableType(Object variable) {
        if (variable instanceof String) {
            System.out.println(variable + " is a String.");
        } else if (variable instanceof Integer) {
            System.out.println(variable + " is an Integer.");
        } else if (variable instanceof Double) {
            System.out.println(variable + " is a Double.");
        } else if (variable instanceof Boolean) {
            System.out.println(variable + " is a Boolean.");
        } else if (variable instanceof Character) {
            System.out.println(variable + " is a Character.");
        } else {
            System.out.println(variable + " is of unknown type.");
  1. You can see that we wrote java code here to check the variable's type.
  2. We create a class called "VariableTypeChecker" that has the methods "main" and "checkVariableType" defined in it.
  3. Initializing variables of various sorts, including a String, an integer, a double, a boolean, and a character, is done in the "main" method.
  4. The "checkVariableType" method is then repeatedly used with each variable passed in as a parameter.
  5. The "checkVariableType" method accepts any object type because it takes an object type argument.
  6. The type of object supplied as an input to this method is determined using the "instanceof" operator.
  7. The "instanceof" operator identifies whether an object is a member of a specific class type or a subclass type.
  8. The "checkVariableType" method then determines whether the object supplied as an argument is an instance of the String, Integer, Double, Boolean, or Character classes.
  9. A message identifying the type of the variable is printed out if it is an instance of any of these classes.
  10. The method provides out a message informing the user that the type of the object is unknown if it is not one of these.

Conclusion :-

Thus, we have successfully acquired the knowledge necessary to determine the data type of a variable in Java.

As well as how to design a function to check for various object kinds, we learnt how to utilise the "instanceof" operator to determine a variable's type.

This method can be used to create adaptable, reliable programs that can manage different kinds of data. Java programmers can build more effective code that can handle a larger variety of data types by learning how to check the type of a variable.

I hope this article on how to check type of variable in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪