How to Check Type of a Variable in Java

Hassan Saeed Feb 02, 2024
  1. Use getClass().getSimpleName() to Check the Type of a Variable in Java
  2. Use getClass().getTypeName() to Check the Type of a Variable in Java
  3. Conclusion
How to Check Type of a Variable in Java

In Java, knowing the type of a variable or object is crucial for writing efficient and flexible code.

Two commonly used approaches to check the type of a variable involve the getClass().getSimpleName() and getClass().getTypeName() methods.

In this article, we will introduce how to utilize them to get the types of variables and objects in Java.

Use getClass().getSimpleName() to Check the Type of a Variable in Java

We can check the type of a variable in Java by calling getClass().getSimpleName() method via the variable.

These methods provide insights into the runtime class of an object and its fully qualified type name, respectively.

The getClass() method is a fundamental feature available for all objects in Java. It provides information about the runtime class of an object, allowing you to understand the type of the object dynamically. Here’s the syntax for using the getClass() method:

Class<?> objectClass = object.getClass();

Now, let’s break down how to use getClass() in detail:

Step 1: Create an Object

To begin, you need an object whose type you want to ascertain. For this example, let’s create an instance of the String class:

Object myObject = new String("Hello, Java!");

Step 2: Call the getClass() Method

Next, you can invoke the getClass() method on the object:

Class<?> objectClass = myObject.getClass();

The objectClass variable now holds a Class object representing the runtime class of myObject.

Step 3: Explore Class Information

With the Class object at your disposal, you can explore various aspects of the class, such as its name, modifiers, superclass, and implemented interfaces. For instance, to retrieve the simple name of the class (without package information), you can use getSimpleName():

String className = objectClass.getSimpleName();

This gives you the simple name of the class, which, in this case, is "String".

Complete Example Code

Here’s the complete code for using getClass():

public class GetTypeUsingGetClass {
  public static void main(String[] args) {
    Object myObject = new String("Hello, Java!");

    Class<?> objectClass = myObject.getClass();
    String className = objectClass.getSimpleName();

    System.out.println("Object Type: " + className);
  }
}

Running this code will output:

Object Type: String

The below example illustrates the use of this method on an array.

public class MyClass {
  public static void main(String args[]) {
    String[] arr = new String[5];
    System.out.println(arr.getClass().getSimpleName());
  }
}

Output:

String[]

This method is callable by objects only; therefore, to check the type of primitive data types, we need to cast the primitive to Object first. The below example illustrates how to use this function to check the type of non-primitive data types.

public class MyClass {
  public static void main(String args[]) {
    int x = 5;
    System.out.println(((Object) x).getClass().getSimpleName());
  }
}

Output:

Integer

Use getClass().getTypeName() to Check the Type of a Variable in Java

Starting from Java 8, a new method called getTypeName() is available to directly provide the fully qualified type name as a String. It simplifies the process of obtaining the type name, as it does not require interacting with the Class object. Here’s the syntax for using the getTypeName() method:

String typeName = object.getClass().getTypeName();

Let’s delve into how to use getTypeName():

Step 1: Create an Object

Just like before, you need an object to determine its type:

Object myObject = new String("Hello, Java!");

Step 2: Call the getTypeName() Method

Invoke the getTypeName() method on the object:

String typeName = myObject.getClass().getTypeName();

The typeName variable now holds a String representing the fully qualified type name of the object.

Example Code

Here’s the complete code for using getTypeName():

public class GetTypeUsingGetTypeName {
  public static void main(String[] args) {
    Object myObject = new String("Hello, Java!");

    String typeName = myObject.getClass().getTypeName();

    System.out.println("Object Type: " + typeName);
  }
}

Running this code will output:

Object Type: java.lang.String

Conclusion

In Java programming, getting variable types is essential for building robust and adaptable applications. The getClass().getSimpleName() and getClass().getTypeName() methods provide solutions for checking variable types efficiently.

These methods empower developers to make informed decisions, classify objects based on their types, and write code that adapts dynamically to various object types. Whether you need a simple or fully qualified type name, Java offers the tools you need to gain insights into your variables’ types, making your code more versatile and responsive.

Related Article - Java Data Type