The Class object representing the type of your object. The
Class object can be queried for useful run time information.
There’s a third form of RTTI in Java. This is the keyword instanceof, which tells you if an object is an instance of a particular type. It returns a boolean so you use it in the form of a question, like this:
if(x instanceof Dog)
((Dog)x).bark();
The if statement checks to see if the object x belongs to the class Dog before casting x to a Dog. It’s important to use instanceof before a downcast when you don’t have other information that tells you the type of the object; otherwise, you’ll end up with a ClassCastException.
Ordinarily, you might be hunting for one type (triangles to turn purple, for example), but you can easily tally all of the objects by using instanceof. Suppose you have a family of Pet classes:
//: c10:Pet.java
package c10;
public class Pet {} ///:~
//: c10:Dog.java
package c10;
public class Dog extends Pet {} ///:~
//: c10:Pug.java
package c10;
public class Pug extends Dog {} ///:~
//: c10:Cat.java
package c10;
public class Cat extends Pet {} ///:~
//: c10:Rodent.java
package c10;
public class Rodent extends Pet {} ///:~
//: c10:Gerbil.java
package c10;
public class Gerbil extends Rodent {} ///:~
//: c10:Hamster.java
package c10;
public class Hamster extends Rodent {} ///:~
In the coming example, we want to keep track of the number of any particular type of Pet, so we’ll need a class that holds this number in an int. You can think of it as a modifiable Integer:
//: c10:Counter.java
package c10;
public class Counter {
int i;
public String toString() { return Integer.toString(i); }
} ///:~
Next, we need a tool that holds two things together: an indicator of the Pet type and a Counter to hold the pet quantity. That is, we want to be able to say “how may Gerbil objects are there?” An ordinary array won’t work here, because you refer to objects in an array by their index numbers. What we want to do here is refer to the objects in the array by their Pet type. We want to associate Counter objects with Pet objects. There is a standard data structure , called an associative array, for doing exactly this kind of thing. Here is an extremely simple version:
//: c10:AssociativeArray.java
// Associates keys with values.
package c10;
import com.bruceeckel.simpletest.*;
public class AssociativeArray {
private static Test monitor = new Test();
private Object[][] pairs;
private int index;
public AssociativeArray(int length) {
pairs = new Object[length][2];
}
public void put(Object key, Object value) {
if(index >= pairs.length)
throw new ArrayIndexOutOfBoundsException();
pairs[index++] = new Object[] { key, value };
}
public Object get(Object key) {
for(int i = 0; i < index; i++)
if(key.equals(pairs[i][0]))
return pairs[i][1];
throw new RuntimeException("Failed to find key");
}
public String toString() {
String result = "";
for(int i = 0; i < index; i++) {
result += pairs[i][0] + " : " + pairs[i][1];
if(i < index - 1) result += "\n";
}
return result;
}
public static void main(String[] args) {
AssociativeArray map = new AssociativeArray(6);
map.put("sky", "blue");
map.put("grass", "green");
map.put("ocean", "dancing");
map.put("tree", "tall");
map.put("earth", "brown");
map.put("sun", "warm");
try {
map.put("extra", "object"); // Past the end
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Too many objects!");
}
System.out.println(map);
System.out.println(map.get("ocean"));
monitor.expect(new String[] {
"Too many objects!",
"sky : blue",
"grass : green",
"ocean : dancing",
"tree : tall",
"earth : brown",
"sun : warm",
"dancing"
});
}
} ///:~
Your first observation might be that this appears to be a general-purpose tool, so why not put it in a package like com.bruceeckel.tools? Well, it is indeed a general-purpose tool—so useful, in fact, that java.util contains a number of associative arrays (which are also called maps) that do a lot more than this one does, and do it a lot faster. A large portion of Chapter 11 is devoted to associative arrays, but they are significantly more complicated, so using this one will keep things simple and at the same time begin to familiarize you with the value of associative arrays.
In an associative array, the “indexer” is called a key, and the associated object is called a value. Here, we associate keys and values by putting them in an array of two-element arrays, which you see here as pairs. This will just be a fixed-length array that is created in the constructor, so we need index to make sure we don’t run off the end. When you put( ) in a new key-value pair, a new two-element array is created and inserted at the next available location in pairs. If index is greater than or equal to the length of pairs, then an exception is thrown.
To use the get( ) method, you pass in the key that you want it to look up, and it produces the associated value as the result or throws an exception if it can’t be found. The get( ) method is using what is possibly the least efficient approach imaginable to locate the value: starting at the top of the array and using equals( ) to compare keys. But the point here is simplicity, not efficiency, and the real maps in Chapter 11 have solved the performance problems, so we don’t need to worry about it here.
The essential methods in an associative array are put( ) and get( ), but for easy display, toString( ) has been overridden to print the key-value pairs. To show that it works, main( ) loads an AssociativeArray with pairs of strings and prints the resulting map, followed by a get( ) of one of the values.
Now that all the tools are in place, we can use instanceof to count Pets:
//: c10:PetCount.java
// Using instanceof.
package c10;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class PetCount {
private static Test monitor = new Test();
private static Random rand = new Random();
static String[] typenames = {
"Pet", "Dog", "Pug", "Cat",
"Rodent", "Gerbil", "Hamster",
};
// Exceptions thrown to console:
public static void main(String[] args) {
Object[] pets = new Object[15];
try {
Class[] petTypes = {
Class.forName("c10.Dog"),
Class.forName("c10.Pug"),
Class.forName("c10.Cat"),
Class.forName("c10.Rodent"),
Class.forName("c10.Gerbil"),
Class.forName("c10.Hamster"),
};
for(int i = 0; i < pets.length; i++)
pets[i] = petTypes[rand.nextInt(petTypes.length)]
.newInstance();
} catch(InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch(IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
} catch(ClassNotFoundException e) {
System.out.println("Cannot find class");
System.exit(1);
}
AssociativeArray map =
new AssociativeArray(typenames.length);
for(int i = 0; i < typenames.length; i++)
map.put(typenames[i], new Counter());
for(int i = 0; i < pets.length; i++) {
Object o = pets[i];
if(o instanceof Pet)
((Counter)map.get("Pet")).i++;
if(o instanceof Dog)
((Counter)map.get("Dog")).i++;
if(o instanceof Pug)
((Counter)map.get("Pug")).i++;
if(o instanceof Cat)
((Counter)map.get("Cat")).i++;
if(o instanceof Rodent)
((Counter)map.get("Rodent")).i++;
if(o instanceof Gerbil)
((Counter)map.get("Gerbil")).i++;
if(o instanceof Hamster)
((Counter)map.get("Hamster")).i++;
}
// List each individual pet:
for(int i = 0; i < pets.length; i++)
System.out.println(pets[i].getClass());
// Show the counts:
System.out.println(map);
monitor.expect(new Object[] {
new TestExpression("%% class c10\\."+
"(Dog|Pug|Cat|Rodent|Gerbil|Hamster)",
pets.length),
new TestExpression(
"%% (Pet|Dog|Pug|Cat|Rodent|Gerbil|Hamster)" +
" : \\d+", typenames.length)
});
}
} ///:~
In main( ) an array petTypes of Class objects is created using Class.forName( ). Since the Pet objects are in package c09, the package name must be used when naming the classes.
Next, the pets array is filled by randomly indexing into petTypes and using the selected Class object to generate a new instance of that class with Class.newInstance( ), which uses the default (no-arg) class constructor to generate the new object.
Both forName( ) and newInstance( ) can generate exceptions, which you can see handled in the catch clauses following the try block. Again, the names of the exceptions are relatively useful explanations of what went wrong (IllegalAccessException relates to a violation of the Java security mechanism).
After creating the AssociativeArray, it is filled with key-value pairs of pet names and Counter objects. Then each Pet in the randomly-generated array is tested and counted using instanceof. The array and AssociativeArray are printed so you can compare the results.
There’s a rather narrow restriction on instanceof: You can compare it to a named type only, and not to a Class object. In the preceding example you might feel that it’s tedious to write out all of those instanceof expressions, and you’re right. But there is no way to cleverly automate instanceof by creating an array of Class objects and comparing it to those instead (stay tuned—you’ll see an alternative). This isn’t as great a restriction as you might think, because you’ll eventually understand that your design is probably flawed if you end up writing a lot of instanceof expressions.
Of course, this example is contrived—you’d probably put a static field in each type and increment it in the constructor to keep track of the counts. You would do something like that if you had control of the source code for the class and could change it. Since this is not always the case, RTTI can come in handy.
Using class literals
It’s interesting to see how the PetCount.java example can be rewritten using class literals. The result is cleaner in many ways:
//: c10:PetCount2.java
// Using class literals.
package c10;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class PetCount2 {
private static Test monitor = new Test();
private static Random rand = new Random();
public static void main(String[] args) {
Object[] pets = new Object[15];
Class[] petTypes = {
// Class literals:
Pet.class,
Dog.class,
Pug.class,
Cat.class,
Rodent.class,
Gerbil.class,
Hamster.class,
};
try {
for(int i = 0; i < pets.length; i++) {
// Offset by one to eliminate Pet.class:
int rnd = 1 + rand.nextInt(petTypes.length - 1);
pets[i] = petTypes[rnd].newInstance();
}
} catch(InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch(IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
}
AssociativeArray map =
new AssociativeArray(petTypes.length);
for(int i = 0; i < petTypes.length; i++)
map.put(petTypes[i].toString(), new Counter());
for(int i = 0; i < pets.length; i++) {
Object o = pets[i];
if(o instanceof Pet)
((Counter)map.get("class c10.Pet")).i++;
if(o instanceof Dog)
((Counter)map.get("class c10.Dog")).i++;
if(o instanceof Pug)
((Counter)map.get("class c10.Pug")).i++;
if(o instanceof Cat)
((Counter)map.get("class c10.Cat")).i++;
if(o instanceof Rodent)
((Counter)map.get("class c10.Rodent")).i++;
if(o instanceof Gerbil)
((Counter)map.get("class c10.Gerbil")).i++;
if(o instanceof Hamster)
((Counter)map.get("class c10.Hamster")).i++;
}
// List each individual pet:
for(int i = 0; i < pets.length; i++)
System.out.println(pets[i].getClass());
// Show the counts:
System.out.println(map);
monitor.expect(new Object[] {
new TestExpression("%% class c10\\." +
"(Dog|Pug|Cat|Rodent|Gerbil|Hamster)",
pets.length),
new TestExpression("%% class c10\\." +
"(Pet|Dog|Pug|Cat|Rodent|Gerbil|Hamster) : \\d+",
petTypes.length)
});
}
} ///:~
Here, the typenames array has been removed in favor of getting the type name strings from the Class object. Notice that the system can distinguish between classes and interfaces.
You can also see that the creation of petTypes does not need to be surrounded by a try block since it’s evaluated at compile time and thus won’t throw any exceptions, unlike Class.forName( ).
When the Pet objects are dynamically created, you can see that the random number is restricted so it is between one and petTypes.length and does not include zero. That’s because zero refers to Pet.class, and presumably a generic Pet object is not interesting. However, since Pet.class is part of petTypes, the result is that all of the pets get counted.
A dynamic instanceof
The Class.isInstance method provides a way to dynamically call the instanceof operator. Thus, all those tedious instanceof statements can be removed in the PetCount example:
//: c10:PetCount3.java
// Using isInstance()
package c10;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class PetCount3 {
private static Test monitor = new Test();
private static Random rand = new Random();
public static void main(String[] args) {
Object[] pets = new Object[15];
Class[] petTypes = {
// Class literals:
Pet.class,
Dog.class,
Pug.class,
Cat.class,
Rodent.class,
Gerbil.class,
Hamster.class,
};
try {
for(int i = 0; i < pets.length; i++) {
// Offset by one to eliminate Pet.class:
int rnd = 1 + rand.nextInt(petTypes.length - 1);
pets[i] = petTypes[rnd].newInstance();
}
} catch(InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch(IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
}
AssociativeArray map =
new AssociativeArray(petTypes.length);
for(int i = 0; i < petTypes.length; i++)
map.put(petTypes[i].toString(), new Counter());
for(int i = 0; i < pets.length; i++) {
Object o = pets[i];
// Using Class.isInstance() to eliminate
// individual instanceof expressions:
for(int j = 0; j < petTypes.length; ++j)
if(petTypes[j].isInstance(o))
((Counter)map.get(petTypes[j].toString())).i++;
}
// List each individual pet:
for(int i = 0; i < pets.length; i++)
System.out.println(pets[i].getClass());
// Show the counts:
System.out.println(map);
monitor.expect(new Object[] {
new TestExpression("%% class c10\\." +
"(Dog|Pug|Cat|Rodent|Gerbil|Hamster)",
pets.length),
new TestExpression("%% class c10\\." +
"(Pet|Dog|Pug|Cat|Rodent|Gerbil|Hamster) : \\d+",
petTypes.length)
});
}
} ///:~
You can see that the isInstance( ) method has eliminated the need for the instanceof expressions. In addition, this means that you can add new types of pets simply by changing the petTypes array; the rest of the program does not need modification (as it did when using the instanceof expressions).
instanceof vs. Class
equivalence
When querying for type information, there’s an important difference between either form of instanceof (that is, instanceof or isInstance( ), which produce equivalent results) and the direct comparison of the Class objects. Here’s an example that demonstrates the difference:
//: c10:FamilyVsExactType.java
// The difference between instanceof and class
package c10;
import com.bruceeckel.simpletest.*;
class Base {}
class Derived extends Base {}
public class FamilyVsExactType {
private static Test monitor = new Test();
static void test(Object x) {
System.out.println("Testing x of type " +
x.getClass());
System.out.println("x instanceof Base " +
(x instanceof Base));
System.out.println("x instanceof Derived " +
(x instanceof Derived));
System.out.println("Base.isInstance(x) " +
Base.class.isInstance(x));
System.out.println("Derived.isInstance(x) " +
Derived.class.isInstance(x));
System.out.println("x.getClass() == Base.class " +
(x.getClass() == Base.class));
System.out.println("x.getClass() == Derived.class " +
(x.getClass() == Derived.class));
System.out.println("x.getClass().equals(Base.class)) "+
(x.getClass().equals(Base.class)));
System.out.println(
"x.getClass().equals(Derived.class)) " +
(x.getClass().equals(Derived.class)));
}
public static void main(String[] args) {
test(new Base());
test(new Derived());
monitor.expect(new String[] {
"Testing x of type class c10.Base",
"x instanceof Base true",
"x instanceof Derived false",
"Base.isInstance(x) true",
"Derived.isInstance(x) false",
"x.getClass() == Base.class true",
"x.getClass() == Derived.class false",
"x.getClass().equals(Base.class)) true",
"x.getClass().equals(Derived.class)) false",
"Testing x of type class c10.Derived",
"x instanceof Base true",
"x instanceof Derived true",
"Base.isInstance(x) true",
"Derived.isInstance(x) true",
"x.getClass() == Base.class false",
"x.getClass() == Derived.class true",
"x.getClass().equals(Base.class)) false",
"x.getClass().equals(Derived.class)) true"
});
}
} ///:~
The test( ) method performs type checking with its argument using both forms of instanceof. It then gets the Class reference and uses == and equals( ) to test for equality of the Class objects. Reassuringly, instanceof and isInstance( ) produce exactly the same results, as do equals( ) and ==. But the tests themselves draw different conclusions. In keeping with the concept of type, instanceof says “are you this class, or a class derived from this class?” On the other hand, if you compare the actual Class objects using ==, there is no concern with inheritance—it’s either the exact type or it isn’t.