Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
What Can an Object[] Array Hold in Java?
Last updated: October 6, 2025
1. Overview
In Java, one common challenge that beginners face is understanding arrays and how they interact with the type system. For instance, if we declare a String[] array, it stores strings, whereas declaring an Object[] array is not as straightforward. To utilize arrays effectively, we therefore need to know their capabilities as well as limitations.
In this tutorial, we’ll focus on the Object[] array and explore what it can hold.
2. Arrays and the Type System
Arrays help to store multiple elements of the same type in a contiguous block of memory. That type can either be a primitive (int, double) or a reference (String, Array) type.
Now, since Java is a strongly typed language, an array declared with a specific type can only hold elements of that type or its subclasses:
String[] names = {"Samuel", "Bob", "Chris"};
names[0] = "Alice";
names[1] = 20;
Above, names can only accept String objects since it’s a String[] array. When we try to insert an Integer, in this case, names[1] = 20, or any unrelated type, we get a compile-time error.
3. The Special Role of Object in Java
In Java’s inheritance tree, the Object class is at the very top. To explain, every other class, whether built-in or custom, extends Object either directly or indirectly. As a consequence, Object is often referred to as the root of the class hierarchy.
For the reason discussed above, a variable declared as type Object can hold a reference to any object:
Object obj = "Hello"; // A String stored as Object
obj = 42; // An Integer (autoboxed) stored as Object
obj = new java.util.Date(); // A Date object stored as Object
With this flexibility, we can assign an instance of any class to an Object variable. Once we do this, however, we lose direct access to the specific methods of that class. Conveniently, if we need to use type-specific functionality later, we can cast the object back to its original class:
Object obj = "Hello";
String str = (String) obj; // Cast back to String
System.out.println(str.toUpperCase()); // Now we can use String methods
Thus, Object can reference any class instance, but casting is needed if we plan to utilize the specific methods of the original class.
4. What Does This Mean for Object[]?
By understanding the special role of Object, the idea of an Object[] array now becomes clearer.
When we declare an Object[] array, we inform Java that the array can store references of type Object. Therefore, the Object[] array can hold references to any kind of object, including:
- Built-in classes such as String, Integer, Double, or Boolean
- Custom classes that we create, such as Person
- Other arrays, since arrays themselves are also projects
- Wrapper objects for primitives since Java automatically boxes primitives into their wrapper types when needed, for instance, 42 becomes an Integer
- null references since null is a valid placeholder for any object reference
Let’s look at a simple example that demonstrates how an Object[] can hold different types of objects:
Object[] values = new Object[5];
values[0] = "Hello"; // String
values[1] = 42; // Autoboxed Integer
values[2] = 3.14; // Autoboxed Double
values[3] = new int[]{1, 2, 3}; // int[] array
values[4] = new Person("Alice", 30); // Custom class
Here, the values array stores five different types of objects, showing the flexibility of the Object[] array compared to arrays of a specific type, like String[].
However, similar to a single Object reference, when retrieving elements from an Object[] array, we need to cast back if we want to use type-specific methods:
String greeting = (String) values[0];
System.out.println(greeting.toUpperCase());
In this example, Java only sees values[0] as a plain object without the cast, and as a result, we cannot call methods like toUpperCase() directly.
5. What Object[] Cannot Hold Directly
Even though an Object[] array is very flexible, there are a few limitations we need to keep in mind.
5.1. Primitive Types Cannot Be Stored Directly
In Java, primitive values like int, boolean, or double are not objects. Hence, they cannot be stored directly in an Object[] array:
Object[] arr = new Object[1];
arr[0] = 5;
The command above compiles and runs, but not because the primitive value 5 was stored in the array. Instead, Java automatically converts the value into an Integer object through a feature called autoboxing.
So, while it looks like Java stores an int into the array, what it really stores is an Integer wrapper object.
5.2. No Built-in Safety
Another limitation is that Object[] doesn’t enforce type consistency. For instance, we can mix different types within the same array:
Object[] mixed = new Object[2];
mixed[0] = "Hello"; // String
mixed[1] = 42; // Integer
Although this flexibility is powerful, it also introduces risk. For instance, if we later cast incorrectly, we get a ClassCastException at runtime:
String text = (String) mixed[1]; // Throws ClassCastException
Above, mixed[1] contains an Integer, but we tried to cast it to a String.
6. Covariance in Arrays
Arrays in Java are covariant, meaning if one type is a subclass of another, then an array of the subclass is also treated as an array of the superclass.
For instance, since Integer and Double are subclasses of Number, an Integer[] or Double[] can be used where a Number[] is expected:
Number[] numbers = new Number[2];
numbers[0] = 10; // Integer fits
numbers[1] = 3.14; // Double fits
Here, both values fit since Integer and Double both inherit from Number. From this demonstration, we can infer two key points:
- An array declared with a specific type can hold values of that type and its subclasses, for instance, a Number[] array can hold Integer or Double
- An Object[] can hold any class, since every class is a subclass of Object
Above, we show how arrays follow inheritance rules, with Object[] being the most flexible since every class in Java extends Object.
With this flexibility, however, comes a risk. If we assign a more specific array like String[] to a broader type reference like Object[], the compiler allows it, whereas inserting the wrong type only fails at runtime:
String[] strings = new String[2];
Object[] objs = strings;
objs[0] = 100; // Compiles fine, but throws ArrayStoreException at runtime
While covariance makes arrays flexible, it can also introduce runtime errors if we try to store the wrong type.
7. Conclusion
In this article, we explored what an Object[] Array can hold in Java.
We learned that an Object[] array can store references to any class in Java since every class in Java extends Object. For instance, built-in types, custom classes, arrays, and even null. Primitive types, however, are not stored directly. Instead, Java wraps them in their corresponding wrapper classes through autoboxing.
Additionally, Object[] doesn’t enforce type safety, meaning that incorrect casts are only caught at runtime with a ClassCastException.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.















