56. Hooking instanceof in a nutshell
Having an object (o) and a type (t), we can use the instanceof operator to test if o is of type t by writing o instanceof t. This is a boolean operator that is very useful to ensure the success of a subsequent casting operation. For instance, check the following:
interface Furniture {};
class Plywood {};
class Wardrobe extends Plywood implements Furniture {};
instanceof returns true if we test the object (for instance, Wardrobe) against the type itself:
Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Wardrobe) { } // true
Plywood plywood = new Plywood();
if(plywood instanceof Plywood) { } // true
instanceof returns true if the tested object (for instance, Wardrobe) is an instance of a subclass of the type (for instance Plywood):
Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Plywood) {} // true
instanceof returns true if the tested object (for instance, Wardrobe) implements the interface represented by the type (for instance, Furniture):
Wardrobe wardrobe = new Wardrobe();
if(wardrobe instanceof Furniture) {} // true
Based on this, consider the following note:
Important note
The logic behind instanceof relies on the IS-A relationship (this is detailed in The Complete Coding Interview Guide in Java, Chapter 6, What is inheritance?). In a nutshell, this relationship is based on interface implementation or class inheritance. For instance, wardrobe instanceof Plywood returns true because Wardrobe extends Plywood, so Wardrobe IS A Plywood. Similarly, Wardrobe IS A Furniture. On the other hand, Plywood IS-not-A Furniture, so plywood instanceof Furniture returns false. In this context, since every Java class extends Object, we know that foo instanceof Object returns true as long as foo is an instance of a Java class. In addition, null instanceof Object (or any other object) returns false, so this operator doesn’t require an explicit null check.
Finally, keep in mind that instanceof works only with reified types (reified type information is available at runtime), which include:
- Primitive types (
int,float) - Raw types (
List,Set) - Non-generic classes/interfaces (
String) - Generic types with unbounded wildcards (
List<?>,Map<?, ?>) - Arrays of reifiable types (
String[],Map<?, ?>[],Set<?>[])
This means that we cannot use the instanceof operator (or casts) with parameterized types because the type erasures alter all type parameters in generic code, so we cannot say which parameterized type for a generic type is in use at runtime.