DEV Community

Cover image for Java Type Comparison
Grilled Pasta
Grilled Pasta

Posted on • Originally published at theflashreads.com

Java Type Comparison

Original post: Flashreads Blog - Type Comparison

The most common way to determine if a given object is an instance of a given class, superclass or interface, is by using the binary operator instanceof. It includes implicit null check and generates a compile-time error if the types are not related. But it doesn't allow primitives and requires the types to be know at compile time.

If you want to check dynamically at runtime

Use the equivalent method boolean isInstance(Object obj) in Class. It also includes null check, but allows for primitives:

    a instanceof B 
    // returns false for null
    null instanceof B

    a.getClass().isInstance(b);
    // commonly used for generics
    Class<T> type = b.getClass();
    type.isInstance(a);

    // Note that the parameter is autoboxed to type Integer
    int x = 4;
    Integer.class.isInstance(x);
Enter fullscreen mode Exit fullscreen mode

Check compatibility of two types

When you need to check subtyping relation use the method boolean isAssignableFrom(Class<?> cls) in Class. It may throw NullPointerException.

    // is it possible to B b = new A()
    Class<?> aClass = CharSequence.class;
    Class<?> bClass = String.class;
    bClass.isAssignableFrom(aClass());

    // works for arrays
    CharSequence[].class.isAssignableFrom(String[].class); // true
    Integer[].class.isAssignableFrom(String[].class); //false
Enter fullscreen mode Exit fullscreen mode

Pattern Matching (Java 14)

    if(a instanceof B b) {
        // b is casted
        b.toString();
    }
Enter fullscreen mode Exit fullscreen mode

Special types

    // Enums
    enum Color { WHITE, GRAY, BLACK }

    Color.class.isEnum(); // Enum.class.isAssignableFrom(Color.class); 
    Color.WHITE instanceof Enum; // true

    // Arrays
    String[].class.isArray();
    // get the type of the variables in an array (null if obj is not an array)
    Class<?> componentType = obj.getComponentType(); 

    // Primitives
    int.class.isPrimitive();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)