Explain Codes LogoExplain Codes Logo

How to check type of variable in Java?

java
type-checking
instanceof-operator
reflection-api
Nikita BarsukovbyNikita Barsukov·Jan 19, 2025
TLDR
boolean isInteger = myVariable instanceof Integer;

You can determine the type of a variable with the instanceof operator. The above code checks if myVariable is an Integer and assigns either true or false to isInteger.

A beginner's guide to variable types in Java

In Java, a statically typed language, the type of each variable and expression is established at compile time. Think of Java variables as Matryoshka dolls, each holding another within it. The top level Object can harbor a Number, which in turn can nest an Integer. This composition makes it possible to identify the type of a variable.

The instanceof operator: your first move

When you need to verify whether your variable is an instance of a specific class, you call upon the instanceof operator:

if (myVariable instanceof Double) { /* I'm Double or nothing! */ }

Need the exact Class? getClass has got you

If you need to pinpoint the exact type of the variable, the getClass() method retrieves the class object, and the getName() or getSimpleName() would get you the class name::

String className = myVariable.getClass().getName(); // The class name in its full glory. String simpleName = myVariable.getClass().getSimpleName(); // Just the cool nickname - 'Integer', 'Double', etc.

Reflection API's X-ray vision

Java's Reflection API is a powerful tool that allows you to inspect classes, interfaces, fields, and methods at runtime. However, keep in mind that it's more resource-intensive than direct type checks:

Class<?> clazz = myVariable.getClass(); // clazz is now a treasure map to information about the type

Thinking the object-oriented way

Remember the object-oriented principle of polymorphism before going on a type checking spree. Checking for specific types might work against you if downcasting is involved. More often than not, your code should work with the interface, not the implementation.

Deep dive: Better practices, tricks, and pitfalls

Overloading methods: Type distinctions done smartly

Method overloading allows you to tailor your methods based on the variable type:

void process(Integer num) { /* integer mathing */ } void process(Double num) { /* floating-point wrangling */ }

Array type checks: It's simple!

Checking the type for arrays uses the instanceof operator:

boolean isStringArray = myArray instanceof String[];

Working with primitive types: Play it smart, avoid boxing

Wrapper classes like Integer and Double provide methods for dealing with primitive types. Use them wisely to avoid boxing and unboxing overheads.

int number = Integer.parseInt("123"); // Perhaps the easiest math test ever?

Making the most of Java SDK features

Java's SDK houses incredibly useful features like Class.isAssignableFrom() and Class.isInstance(). These methods offer the flexibility needed when the trusty instanceof operator falls short.