Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

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.
Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2025 – NPI (All)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)