Friday, February 4, 2011

Chapter 24: instanceof Operator

The instanceof operator is used for object reference variables only, and you can use it to check whether an object is of a particular type. By type, we mean class or interface type—in other words, if the object referred to by the variable on the left side of the operator passes the IS-A test for the class or interface type on the right side. The following simple example

public static void main(String[] args) {
String s = new String("test");
if (s instanceof String) {
System.out.print("s is a String");

prints this: s is a String

Even if the object being tested is not an actual instantiation of the class type on the right side of the operator, instanceof will still return true if the object being compared is assignment compatible with the type on the right.

The following example demonstrates a common use for instanceof: testing an object to see if it’s an instance of one of its subtypes, before attempting a “downcast”:
class A { }
class B extends A {
public static void main (String [] args) {
A myA = new B();
public static void m2(A a) {
if (a instanceof B)
((B)a).doBstuff(); // downcasting an A reference to a B reference
public static void doBstuff() {
System.out.println("'a' refers to a B");

The preceding code compiles and produces the output:
'a' refers to a B

In examples like this, the use of the instanceof operator protects the program from attempting an illegal downcast.

You can test an object reference against its own class type, or any of its superclasses. This means that any object reference will evaluate to true if you use the instanceof operator against type Object, as follows,

B b = new B();
if (b instanceof Object) {
System.out.print("b is definitely an Object");

which prints this: b is definitely an Object

In addition, it is legal to test whether the null reference is an instance of a class. This will always result in false, of course. For example:

class InstanceTest {
public static void main(String [] args) {
String a = null;
boolean b = null instanceof String;
boolean c = a instanceof String;
System.out.println(b + " " + c);

prints this: false false

Exam Tip:
Look for instanceof questions that test whether an object is an instance of an interface, when the object’s class implements the interface indirectly. An indirect implementation occurs when one of an object’s superclasses implements an interface, but the actual class of the instance does not—for example,
interface Drivable { }
class A implements Drivable { }
class B extends A { }
A a = new A();
B b = new B();

the following are true:
a instanceof Drivable
b instanceof A
b instanceof Drivable // implemented indirectly

An object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object’s superclasses implement the interface.

instanceof Compiler Error

You can’t use the instanceof operator to test across two different class hierarchies. For instance, the following will NOT compile:
class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
Compilation fails—there’s no way d could ever refer to a Cat or a subtype of Cat.

Exam Tip:
Remember that arrays are objects, even if the array is an array of primitives. Watch for questions that look something like this:
int [] nums = new int[3];
if (nums instanceof Object) { } // result is true

An array is always an instance of Object. Any array.

Previous Chapter: Chapter 23 - Relational Operators

Next Chapter: Chapter 25 - Arithmetic Operators

No comments:

Post a Comment

© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.


Google+ Followers