As we have seen, the instanceof type comparison operator effectively determines whether the reference value in the reference on the left-hand side refers to an object whose class is a subtype of the type specified on the right-hand side. At runtime, it is the type of the actual object denoted by the reference on the left-hand side that is compared with the type specified on the right-hand side. In other words, what matters at runtime is the type of the actual object denoted by the reference, not the declared or static type of the reference.

Example 5.22 provides more examples of the instanceof type comparison operator. It is instructive to go through the print statements and understand the results printed by the program. The following facts should be noted:

  • The literal null is not an instance of any reference type, as shown in the print statements at (1), (2), and (16).
  • An instance of a superclass is not an instance of its subclass, as shown in the print statement at (4).
  • An instance of a class is not an instance of a totally unrelated class, as shown in the print statement at (10).
  • An instance of a class is not an instance of an interface type that the class does not implement, as shown in the print statement at (6).
  • Any array of a non-primitive type is an instance of both Object and Object[] types, as shown in the print statements at (14) and (15), respectively.

Example 5.22 Using the instanceof Type Comparison Operator

Click here to view code image

// See
Figure 5.4
,
p. 245
, for inheritance hierarchy.
interface IStack
interface ISafeStack extends IStack
class Stack implements IStack
class SafeStack extends Stack implements ISafeStack

Click here to view code image

public class Identification {
  public static void main(String[] args) {
    Object obj = new Object();
    Stack stack = new Stack(10);
    SafeStack safeStack = new SafeStack(5);
    IStack iStack;
    String strFormat = “(%d)  %-25s instance of %-25s: %s%n”;
    System.out.printf(strFormat, 1,
        null, Object.class, null instanceof Object);      // Always false.
    System.out.printf(strFormat, 2,
        null, IStack.class, null instanceof IStack);      // Always false.
    System.out.printf(strFormat, 3, stack.getClass(), Object.class,
        stack instanceof Object);     // true: instance of subclass of Object.
    System.out.printf(strFormat, 4, obj.getClass(), Stack.class,
        obj instanceof Stack);        // false: Object not subtype of Stack.
    System.out.printf(strFormat, 5, stack.getClass(), Stack.class,
        stack instanceof Stack);      // true: instance of Stack.
    System.out.printf(strFormat, 6, obj.getClass(), IStack.class,
        obj instanceof IStack);       // false: Object does not implement IStack.
    System.out.printf(strFormat, 7, safeStack.getClass(), IStack.class,
        safeStack instanceof IStack); // true: SafeStack implements IStack.
    obj = stack;           // No cast required: assigning subclass to superclass.
    System.out.printf(strFormat, 8, obj.getClass(), Stack.class,
        obj instanceof Stack);        // true: instance of Stack.
    System.out.printf(strFormat, 9, obj.getClass(), IStack.class,
        obj instanceof IStack);       // true: Stack implements IStack.
    System.out.printf(strFormat, 10, obj.getClass(), String.class,
        obj instanceof String);       // false: no relationship.
    iStack = (IStack) obj; // Cast required: assigning superclass to subclass.
    System.out.printf(strFormat, 11, iStack.getClass(), Object.class,
        iStack instanceof Object);       // true: instance of subclass of Object.
    System.out.printf(strFormat, 12, iStack.getClass(), Stack.class,
        iStack instanceof Stack);        // true: instance of Stack.
    String[] strArray = new String[10];
//  System.out.printf(strFormat, 13, strArray.getClass(), String.class,
//      strArray instanceof String);     // Compile-time error: no relationship.
    System.out.printf(strFormat, 14, strArray.getClass(), Object.class,

        strArray instanceof Object);     // true: array subclass of Object.
    System.out.printf(strFormat, 15, strArray.getClass(), Object[].class,
        strArray instanceof Object[]);   // true: array subclass of Object[].
    System.out.printf(strFormat, 16, strArray[0], Object.class,
        strArray[0] instanceof Object);  // false: strArray[0] is null.
    System.out.printf(strFormat, 17, strArray.getClass(), String[].class,
        strArray instanceof String[]);   // true: array of String.

    strArray[0] = “Amoeba strip”;
    System.out.printf(strFormat, 18, strArray[0].getClass(), String.class,
        strArray[0] instanceof String);  // true: strArray[0] instance of String.
  }
}

Output from the program:

Click here to view code image (1)  null                      instance of class java.lang.Object   : false
(2)  null                      instance of interface IStack         : false
(3)  class Stack               instance of class java.lang.Object   : true
(4)  class java.lang.Object    instance of class Stack              : false
(5)  class Stack               instance of class Stack              : true
(6)  class java.lang.Object    instance of interface IStack         : false
(7)  class SafeStack           instance of interface IStack         : true
(8)  class Stack               instance of class Stack              : true
(9)  class Stack               instance of interface IStack         : true
(10)  class Stack               instance of class java.lang.String   : false
(11)  class Stack               instance of class java.lang.Object   : true
(12)  class Stack               instance of class Stack              : true
(14)  class [Ljava.lang.String; instance of class java.lang.Object   : true
(15)  class [Ljava.lang.String; instance of class [Ljava.lang.Object;: true
(16)  null                      instance of class java.lang.Object   : false
(17)  class [Ljava.lang.String; instance of class [Ljava.lang.String;: true
(18)  class java.lang.String    instance of class java.lang.String   : true


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *