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

Since: Java 1.0(1996)

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

GetclassInstanceof.java
class Fighter {}
class KofFighter extends Fighter {}
class OrocFighter extends Fighter {}

class GetclassInstanceof {
    public static void main(String[] args) {
        // Check the type using instanceof.
        Object obj = "Yagami Iori";
        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 "YAGAMI IORI".
        }

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

        // 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.
        Fighter a = new KofFighter();
        System.out.println(a instanceof Fighter); // Prints "true".
        System.out.println(a instanceof KofFighter); // Prints "true".
        System.out.println(a instanceof OrocFighter); // 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".
    }
}

The command looks like this:

javac GetclassInstanceof.java
java GetclassInstanceof
true
false
false
YAGAMI IORI
11
Integer
java.lang.Integer
true
true
false
Double: 3.14

Common Mistakes

Common Mistake 1: Confusing instanceof with null checks

instanceof always returns false for null. Confusing null checks with instanceof can lead to redundant checks or missed null handling.

GetclassInstanceofNg1.java
class GetclassInstanceofNg1 {
    static void greet(Object obj) {
        if (obj == null) {
            System.out.println("null");
        }
        if (obj instanceof String) {
            String name = (String) obj;
            System.out.println("Hello, " + name + "!");
        }
    }

    public static void main(String[] args) {
        greet("Yagami Iori");
        greet(null);
        greet(42);
    }
}

The command looks like this:

javac GetclassInstanceofNg1.java
java GetclassInstanceofNg1
Hello, Yagami Iori!
null

instanceof automatically returns false for null, so a separate null check is unnecessary. instanceof alone handles the check safely.

GetclassInstanceofOk1.java
class GetclassInstanceofOk1 {
    static void greet(Object obj) {
        if (obj instanceof String name) {
            System.out.println("Hello, " + name + "!");
        } else {
            System.out.println("Not a String: " + obj);
        }
    }

    public static void main(String[] args) {
        greet("Kusanagi Kyo");
        greet(null);
        greet(42);
    }
}

The command looks like this:

javac GetclassInstanceofOk1.java
java GetclassInstanceofOk1
Hello, Kusanagi Kyo!
Not a String: null
Not a String: 42

Common Mistake 2: Using getClass() to check inheritance and getting unexpected results

getClass() returns only the exact runtime class and does not consider inheritance. When you want to check whether an object is an instance of a superclass, using getClass() will not match subclass instances.

GetclassInstanceofNg2.java
class Fighter {}
class KofFighter extends Fighter {}

class GetclassInstanceofNg2 {
    public static void main(String[] args) {
        Fighter a = new KofFighter();
        System.out.println(a.getClass() == Fighter.class);
        System.out.println(a instanceof Fighter);
    }
}

The command looks like this:

javac GetclassInstanceofNg2.java
java GetclassInstanceofNg2
false
true

Use instanceof for type checks that include inheritance. To check whether something "is a kind of" something else — like whether Terry Bogard is a Fighter — use instanceof.

GetclassInstanceofOk2.java
class Fighter {}
class KofFighter extends Fighter {}

class GetclassInstanceofOk2 {
    static void checkType(Fighter f) {
        if (f instanceof KofFighter kf) {
            System.out.println("KOF Fighter: " + kf.getClass().getSimpleName());
        } else {
            System.out.println("Generic Fighter: " + f.getClass().getSimpleName());
        }
    }

    public static void main(String[] args) {
        checkType(new KofFighter());
        checkType(new Fighter());
    }
}

The command looks like this:

javac GetclassInstanceofOk2.java
java GetclassInstanceofOk2
KOF Fighter: KofFighter
Generic Fighter: Fighter

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 .