Often in Java programs we use an array of Objects to store data. An Object reference can store a String
, an Integer, any class
instance.
With this kind of array, we can hold references to many different types. We sometimes must test the classes with getClass
. We can cast to an Object array.
This program creates an Object array of four elements. It contains a String
, an Integer, a StringBuilder
and a Double
.
for
-loop. We pass each Object to System.out.println
.println
can display these Objects because each Object has a toString
method. Println
uses String.valueOf
to call this.public class Program { public static void main(String[] args) { // Add different objects to an Object array. Object[] elements = new Object[4]; elements[0] = "cat"; elements[1] = 100; elements[2] = new StringBuilder("abc"); elements[3] = 1.2; // Print the objects in a for-loop. for (Object e : elements) { System.out.println(e); } } }cat 100 abc 1.2
GetClass
, if-else
This example is more complex. The display()
method receives an Object array. It then calls getClass
on each Object reference. This returns a Class
reference.
Class
extends object" variable in an if
-statement. We access each "class
" member from the possible types.String.class
. Next we try Integer.class
, StringBuilder.class
and Double.class
.public class Program { static void display(Object[] array) { for (Object v : array) { // Get the class object for the element. Class<? extends Object> c = v.getClass(); // Test the class against known classes. if (c == String.class) { System.out.println("Found String: " + v); } else if (c == Integer.class) { System.out.println("Found Integer: " + v); } else if (c == StringBuilder.class) { System.out.println("Found StringBuilder: " + v); } else if (c == Double.class) { System.out.println("Found Double: " + v); } } } public static void main(String[] args) { Object[] elements = new Object[4]; elements[0] = "spark"; elements[1] = 500; elements[2] = new StringBuilder("therapeutics"); elements[3] = 63.5; // Pass our object array to the display method. display(elements); } }Found String: spark Found Integer: 500 Found StringBuilder: therapeutics Found Double: 63.5
ClassCastException
We cannot cast an Object array to a more derived array directly. This causes a ClassCastException
. We must handle each element separately.
public class Program { public static void main(String[] args) { Object[] elements = new Object[2]; elements[0] = "cat"; elements[1] = "bird"; // This statement causes an error. String[] values = (String[]) elements; for (String v : values) { System.out.println(v); } } }Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; at Program.main(Program.java:8)
An array of any class
can be cast directly to an Object array. This does not result in an exception. We then must handle each element as an Object reference.
public class Program { public static void main(String[] args) { String[] codes = new String[2]; codes[0] = "ABC10"; codes[1] = "DEF20"; // We can cast a String array to an Object array safely. Object[] values = (Object[]) codes; for (Object v : values) { System.out.println(v); } } }ABC10 DEF20
Java's type hierarchy is powerful. Sometimes we want to treat classes as Objects. This allows us to store many different classes in one Object array.
With an Object array, we must test classes with the getClass
method. Object arrays are harder to use, but they also have benefits. They are sometimes required for arguments.