Object Array Examples
This page was last reviewed on Aug 28, 2023.
Dot Net Perls
Object array. 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.
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.
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.
Then We can test the "Class extends object" variable in an if-statement. We access each "class" member from the possible types.
Detail We test against 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)
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.
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); } } }
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 28, 2023 (edit).
© 2007-2024 Sam Allen.