Java Object Array Examples: For, Cast and getClass

Use Object arrays to store varying types of data. Call getClass to test types and casts to object arrays.
Object arrays. 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.Array
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.
First example. This program creates an Object array of four elements. It contains a String, an Integer, a StringBuilder and a Double.

Then: We loop through the Object array with a for-loop. We pass each Object to System.out.println.

Tip: The println can display these Objects because each Object has a toString method. Println uses String.valueOf to call this.

Java program that uses Object array 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); } } } Output 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.

Then: We can test the "Class extends object" variable in an if-statement. We access each "class" member from the possible types.


Tests: We test against String.class. Next we try Integer.class, StringBuilder.class and Double.class.

Java program that uses Object array, getClass 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); } } Output 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.Exceptions
Java program that uses invalid cast, causes error 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); } } } Output Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; at Program.main(
Cast to Object array. 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.

Warning: This often makes further operations on the array more difficult. But it is sometimes required to pass the data as an argument.

Java program that casts to Object array 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); } } } Output ABC10 DEF20
A summary. 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.
Some problems. 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.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls