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:
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:
Now, check the type of the created variable using the “instanceof” operator and print out the resultant value on the console:
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:
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”:
Print the name of the class with the package that the variable belongs using the “getClass().getName()” method in “System.out.println()”:
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:
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:
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.