Java

How to Check Variable Type in Java

You may know that the variable gives named storage that programs can access. Every variable in Java has a unique type that specifies its memory size, the range of operations that can be performed on the variable, and the range of values stored in memory. To compute data, sometimes you need to check the data type of a variable because the logical operations are performed with the same type of variables.

This write-up will discuss the approaches for checking the type of variable in Java.

How to Check Variable Type in Java?

To check the type of variable, you can follow these methods:

Let’s understand these approaches one by one.

Method 1: Check Variable Type Using instanceof Operator

For checking variable type in Java, there is a feature called the “instanceOf” operator, which is used to check the type of a variable or object. It gives the boolean value to tell whether the variable belongs to the specified type or not.

Syntax

Use the below-given syntax for checking the type of variable using the instanceof operator:

s instanceof String

Here, “s” is the variable, and “String” is the predefined Java wrapper class. The “instanceof” operator checks whether the variable “s” is a String type variable or not.

Example

First, we will declare a String type variable “s” and initialize a String value:

String s = "Welcome";

Now, check the type of the created variable using the “instanceof” operator and print out the resultant value on the console:

System.out.print(s instanceof String);

Output shows the boolean value “true” which means the variable “s” is a type of “String”:

Let’s get the name of the type where the variable belongs.

Method 2: Check Variable Type Using getClass() With getName() Method

There is another method for checking the type of variable using the “getClass()” method of the “Object” class with the “getName()” method. This method outputs the full name of the class with its package name.

Syntax

Follow the given syntax for checking type of variable:

s.getClass().getName()

Here, “s” is the variable whose type needs to be checked.

Example

Firstly, we will declare a String type variable “s” and initialize it a string “Welcome to LinuxHint”:

String s = "Welcome to LinuxHint";

Print the name of the class with the package that the variable belongs using the “getClass().getName()” method in “System.out.println()”:

System.out.println(s.getClass().getName());

In the output, the “java.lang.String” indicates that the variable “s” belong to the “String” class of the “java.lang” package:

The above approach will give the class name with the package. However, if you want to get only the class name, follow the below-given section.

Method 3: Check Variable Type Using getClass() With getSimpleName() Method

For getting the exact name of the class without their package, you can use the “getClass().getSimpleName()” method. It prints the class name from where it belongs.

Syntax

The below-given syntax is used for the checking type of variable in Java:

s.getClass().getSimpleName()

Here, the “s” is the variable whose type will be checked using the “getSimpleName()” method.

Example

We will now consider the same String “s” created in the previous example and print out its class name as follows:

System.out.println(s.getClass().getSimpleName());

As you can see that the invoked method only returned the class name:

We have compiled different ways for checking variable type in Java.

Conclusion

For checking variable type in Java, you can use instanceof operator, getClass().getName() method and the getClass().getSimpleName() method. The getClass().getName() method outputs the name of the class with its package name, while the getSimpleName() method prints the exact name of the class where the variable belongs. Lastly, the instanceof operator displays a boolean value. In this write-up, we discussed the methods to check the type of variable 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.