Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. Objects.toString() / getClass() / instanceof

Objects.toString() / getClass() / instanceof

Operators and methods for checking the actual type of an object at runtime. Use instanceof to check a type, and getClass() to retrieve the actual class. Java 16 and later allows pattern matching to simplify casting.

Syntax

// Check the type with instanceof (returns true if the object is of the specified type or a subclass).
boolean result = object instanceof TypeName;

// Pattern matching instanceof (Java 16+): checks and casts in a single step.
if (object instanceof TypeName variableName) {
    // variableName can be used directly as TypeName.
}

// Retrieve the actual Class object at runtime.
Class<?> clazz = object.getClass();

// Get the simple class name (without package).
String simpleName = object.getClass().getSimpleName();

// Get the fully qualified class name.
String fullName = object.getClass().getName();

Methods and Operators

Method / OperatorDescription
instanceof TypeNameReturns true if the object is an instance of the specified type or a subclass of it. Always returns false for null.
instanceof TypeName variableNamePattern matching instanceof (Java 16+). When the condition is true, binds the automatically cast value to the variable.
getClass()Returns the actual runtime class as a Class<?> object.
getClass().getSimpleName()Returns the simple name of the class (without the package portion).
getClass().getName()Returns the fully qualified class name (e.g., java.lang.String).
getClass().getSuperclass()Returns the direct superclass.
getClass().getInterfaces()Returns an array of the interfaces implemented by the class.

Sample Code

// Check the type using instanceof.
Object obj = "Hello, Java!";
System.out.println(obj instanceof String);  // Prints "true".
System.out.println(obj instanceof Integer); // Prints "false".
System.out.println(null instanceof String); // Prints "false" (null is always false).

// Traditional instanceof with explicit cast.
if (obj instanceof String) {
    String s = (String) obj;
    System.out.println(s.toUpperCase()); // Prints "HELLO, JAVA!".
}

// Pattern matching instanceof (Java 16+).
if (obj instanceof String s) {
    // No cast needed — use s directly.
    System.out.println(s.length()); // Prints "12".
}

// Retrieve class information with getClass().
Object num = 42;
System.out.println(num.getClass().getSimpleName()); // Prints "Integer".
System.out.println(num.getClass().getName());        // Prints "java.lang.Integer".

// Combining polymorphism with instanceof.
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}

Animal a = new Dog();
System.out.println(a instanceof Animal); // Prints "true".
System.out.println(a instanceof Dog);    // Prints "true".
System.out.println(a instanceof Cat);    // Prints "false".

// Pattern matching in a switch statement (Java 21+).
Object value = 3.14;
String desc = switch (value) {
    case Integer i -> "Integer: " + i;
    case Double d  -> "Double: " + d;
    case String s  -> "String: " + s;
    default        -> "Other";
};
System.out.println(desc); // Prints "Double: 3.14".

Notes

Use instanceof to check the actual type of an object in polymorphic code. The pattern matching syntax available in Java 16 and later (instanceof TypeName variableName) lets you combine the check and cast in one line, improving readability.

getClass() returns only the exact runtime class, ignoring inheritance. Use instanceof for type checks that account for inheritance; use getClass() == TypeName.class when you need to verify that the class is exactly the same.

For utility methods of the Objects class, see 'Objects.equals() / Objects.toString() / Objects.requireNonNull()'.

If you find any errors or copyright issues, please .