JavaDevNotes.com

Java Array Of Objects

Object is the root class of all classes in Java. Every class that we use or declare in Java has Object as a super class when traced to the top. All objects, including primitive wrappers, implements the method of the Object class. Array on the other hand is a convenient data structure for holding multiple values with the same class or interface. This post will discuss some caveats about Java Array Of Objects.

What is an Array of Objects?

Being the root class of all and than an Object can hold an instance of any class, it means an array of Java object is an array of any Java objects. Consider the code below where the same array holds instance of String, Integer, and Character. This is a valid code that will compile and run in Java.
public class Test {
    public static void main(String[] args) {
        Object[] objectArray = new Object[3];
        objectArray[0] = "Hello World";
        objectArray[1] = new Integer(10);
        objectArray[2] = new Character('a');
        System.out.println(objectArray[0]);
        System.out.println(objectArray[1]);
        System.out.println(objectArray[2]);
    }
}

The output is expected to be:

Hello World
10
a

The ideal use case of using Array of Object in Java is when we can't narrow down the family of class that will be involved as values. Where tracing the root class has no commonality other than the Object class.

Array of Objects Declaration

We can use the class name Object and square brackets to declare an Array of Objects. There are two ways that we can do this. The first one is by placing the square brackets after the Object class name as shown below:
Object[] thisIsAnObjectArray;

The other way is by placing the square brackets after the variable name in the declaration.

Object thisIsAnObjectArray[];

Both statements will have the same result - declaring an array of Object that is initially null.

Array of Objects Declaration With Size

We can also declare an array of objects with an initial size, let's say 4.

Object[] objectArray = new Object[4];
objectArray[0] = "Java Programming";
objectArray[1] = new Integer(8);
objectArray[2] = new Character('z');
objectArray[3] = new Integer(4);

This is the same as the very first example in this post.

Array of Objects Declaration With Initial Values

We can declare an array of Objects with initial contents or values. We can use the default values by using curly braces. See example below:
Object[] thisIsAObjectArray = {"Java Code", new Integer(4)};
This code will create an array with size 2 and the first item is a String with value "Java Code" and the second item is an integer with value 4. We could add some screen output to check:
public class Test {
    public static void main(String[] args) {
        Object[] thisIsAObjectArray = {"Java Code", new Integer(4)};
        System.out.println( thisIsAObjectArray[0] );
        System.out.println( thisIsAObjectArray[1] );
    }
}
The output of the code should be:
Java Code
4

Create Object Array Instance on the Fly with Initial Values

We can create an instance of object array with initial values anytime, it does not require that we are in declaration. We can do it much later in our code after the declaration. Below is an example.
public class Test {
    public static void main(String[] args) {
        Object[] thisIsAObjectArray;
        int x = 1;
        x++;
        thisIsAObjectArray = new Object[] { "Java Code", new Integer(4) };
        System.out.println(thisIsAObjectArray[0]);
        System.out.println(thisIsAObjectArray[1]);
    }
}
Here we did some operation on a variable x before we created an instance of object array and assign it to thisIsAObjectArray variable. The difference is that we need to add the code "new Object[]", instead of just the curly braces.

Support my blog by buying some of my t-shirt, mug, and phone case designs!