HOWTO · Java

How to Check an Object’s Runtime Type in Java

Use getClass(), getSimpleName(), and getTypeName() to inspect an object’s runtime class in Java.

On this page

Use getClass() to inspect the runtime class of a non-null object — never the variable’s declared type. Call getSimpleName() for the class name without its package, getName() for the package-qualified name, or getTypeName() for a type name such as java.lang.String. Use instanceof when you only need a yes-or-no test instead of a name.

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

Call getClass() on an object to obtain its Class<?> object, then call getSimpleName() to get the class name without package information. The result reflects the object’s runtime class, even when the variable is declared as Object.

Run this example:

public class RuntimeSimpleNames {
  public static void main(String[] args) {
    Object value = "Hello, Java!";
    String[] names = {"Ada", "Lin"};
    int count = 3;
    Object boxedCount = count;

    System.out.println(value.getClass().getSimpleName());
    System.out.println(names.getClass().getSimpleName());
    System.out.println(boxedCount.getClass().getSimpleName());
  }
}

Output:

String
String[]
Integer

Arrays are objects, so getClass() works on them too; a String[] value has the simple name String[]. A primitive such as int has no getClass() method — the primitive section below explains why, and what boxing reports instead.

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

getName() returns the fully qualified name of the runtime class, including its package — use it when the package is needed to tell same-named classes apart. For arrays, however, it returns the JVM’s internal name instead of readable source syntax: each leading [ marks one array dimension, and L<class>; encodes the component type.

Run this example:

public class RuntimeName {
  public static void main(String[] args) {
    Object value = "Hello, Java!";
    String[] names = {"Ada", "Lin"};

    System.out.println(value.getClass().getName());
    System.out.println(names.getClass().getName());
  }
}

Output:

java.lang.String
[Ljava.lang.String;

The second line describes a one-dimensional array of java.lang.String. Because this encoding is hard to read, prefer getSimpleName() or getTypeName() for arrays.

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

getTypeName() returns the type name as a String. For a String object, it includes the package name, unlike getSimpleName().

Run this example:

public class RuntimeTypeName {
  public static void main(String[] args) {
    Object value = "Hello, Java!";
    String typeName = value.getClass().getTypeName();
    System.out.println(typeName);
  }
}

Output:

java.lang.String

Choose getSimpleName() for the short name, getName() for the package-qualified class name, and getTypeName() for a readable type name that also formats arrays readably. None of them reports a variable’s declared type.

Use instanceof to Check Whether an Object Is an Instance of a Type

The instanceof operator answers a yes-or-no question instead of returning a name: is this object an instance of the given type or one of its subtypes? It therefore follows the whole type hierarchy, while comparing getClass() with == tests for one exact runtime class only.

Run this example:

import java.util.ArrayList;
import java.util.List;

public class InstanceofCheck {
  public static void main(String[] args) {
    Object value = new ArrayList<String>();

    System.out.println(value instanceof ArrayList);
    System.out.println(value instanceof List);
    System.out.println(value instanceof String);
    System.out.println(value.getClass() == ArrayList.class);
    System.out.println(value.getClass() == List.class);
  }
}

Output:

true
true
false
true
false

The object is an instance of both ArrayList and the List interface it implements, so both instanceof tests return true; the last comparison returns false because the exact runtime class is ArrayList, not List. Rule of thumb: instanceof accepts subtypes, getClass() equality accepts only the exact class.

Use Pattern Matching for instanceof to Check and Cast in One Step

Since Java 16, instanceof can also declare a pattern variable of the tested type. When the test succeeds, the variable is already cast for you, and it stays in scope only where the test is known to have passed — the separate cast that older code needed disappears.

Run this example:

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

    if (value instanceof String text) {
      System.out.println(text.toUpperCase());
    }

    Object number = 42;
    if (number instanceof String text) {
      System.out.println(text);
    } else {
      System.out.println("Not a string");
    }
  }
}

Output:

HELLO, JAVA!
Not a string

The first test binds text as a ready-to-use String. The second fails — the boxed 42 is an Integer — so the else branch runs.

Check the Type of a Variable That Might Be null

A null reference points to no object, so it has no runtime class. Calling getClass() on it throws a NullPointerException. The instanceof operator is the null-safe test: it simply returns false for null instead of throwing.

Run this example:

public class NullTypeCheck {
  public static void main(String[] args) {
    Object value = null;

    System.out.println(value instanceof String);

    try {
      System.out.println(value.getClass().getSimpleName());
    } catch (NullPointerException e) {
      System.out.println("getClass() failed: value is null");
    }
  }
}

Output:

false
getClass() failed: value is null

Guard with an explicit value == null check or an instanceof test before calling any getClass() method on a value that might be null.

Check the Type of a Primitive Value in Java

Primitive values such as int and double are plain values, not objects: they have no methods and carry no runtime class. Their type is fixed at compile time, and at run time the JVM works with the raw value only — which is why count.getClass() does not compile.

To inspect one at run time, let it be boxed into its wrapper class; the reported type is then the wrapper, never the primitive itself. A class literal still exists for the primitive type — int.class, the same object as Integer.TYPE — but it names the type in the abstract, not a particular value.

Run this example:

public class PrimitiveTypeCheck {
  public static void main(String[] args) {
    int count = 3;
    double price = 9.99;

    Object boxedCount = count;
    Object boxedPrice = price;

    System.out.println(boxedCount.getClass().getSimpleName());
    System.out.println(boxedPrice.getClass().getSimpleName());
    System.out.println(int.class.getSimpleName());
    System.out.println(double.class == Double.TYPE);
  }
}

Output:

Integer
Double
int
true

In short: a primitive’s type is a compile-time fact only. Until boxing creates an object, there is nothing at run time for getClass() or instanceof to inspect.

Conclusion

Use getClass().getSimpleName(), getClass().getName(), or getClass().getTypeName() when you need the name of an object’s exact runtime class, and remember that getName() encodes arrays in JVM syntax. Use instanceof — with a pattern variable on Java 16 or later when you also need the cast — when any subtype should pass, and rely on it for values that might be null. Primitive values have no runtime type to report; box them first and expect the wrapper class, such as Integer for an int.