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 – 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 – 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

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

In Java, a jagged array is a type of multidimensional array where each row can contain a different number of elements. It’s also referred to as “ragged array,” or “array of arrays” because it consists of arrays as its elements, each potentially having a different size.

In this tutorial, we’ll learn how to define, initialize, and work with jagged arrays in Java.

2. Memory Representation

As we know, an array in Java is nothing but an object, the elements of which could be either primitives or references. So, a 2D jagged array in Java can be thought of as an array of one-dimensional arrays.

Let’s see what a 2D jagged array looks like in memory:

memory representation of jagged array

Clearly, multiDimensionalArr[0] holds a reference to a single-dimensional array of size 2, multiDimensionalArr[1] holds a reference to another one-dimensional array of size 3, and so on.

This way, Java makes it possible for us to define and use jagged multi-dimensional arrays.

3. Declaring and Initializing Jagged Array

Below are different ways we can declare and initialize a jagged array.

3.1. Declaring and Initializing in a Single Step

Like other multi-dimensional arrays, we can declare and initialize a jagged array in a single step:

int[][] multiDimensionalArray = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};

Here, we’ve declared and initialized an array of three rows, each having a different number of elements.

3.2. Declaring Without Elements

Also, we can declare a jagged array without initializing it with elements:

int[][] multiDimensionalArray = new int[3][];

Here we declared a jagged array of three rows. In this case, the elements aren’t yet initialized, and by default, each element is set to null.

3.3. Initializing the Elements After Declaration

Next, let’s go further by both declaring and initializing the respective elements within multiDimensionalArr:

multiDimensionalArr[0] = new int[] {1, 2};
multiDimensionalArr[1] = new int[] {3, 4, 5};
multiDimensionalArr[2] = new int[] {6, 7, 8, 9};

Here, we assign specific arrays to each row.

3.4. Declaring and Initializing With Specific Sizes

We can also specify the size of each row without initializing the elements:

multiDimensionalArr[0] = new int[2];
multiDimensionalArr[1] = new int[3];
multiDimensionalArr[2] = new int[4];

Later, we can populate the arrays with values:

multiDimensionalArray[0][0] = 1;
multiDimensionalArray[0][1] = 2;

multiDimensionalArray[1][0] = 3;
multiDimensionalArray[1][1] = 4;
multiDimensionalArray[1][2] = 5;

multiDimensionalArray[2][0] = 6;
multiDimensionalArray[2][1] = 7;
multiDimensionalArray[2][2] = 8;
multiDimensionalArray[2][3] = 9;

3.5. Alternate Initialization Methods

A 2-D jagged array can also be initialized in one line:

int[][] multiDimensionalArray = new int[][] { 
  new int[] { 1, 2 }, 
  new int[] { 3, 4, 5 }, 
  new int[] { 6, 7, 8, 9 } 
};

Or more simply:

int[][] multiDimensionalArray = { 
  new int[] { 1, 2 }, 
  new int[] { 3, 4, 5 }, 
  new int[] { 6, 7, 8, 9 } 
};

Unlike the first, the second initialization omits the new int[][] keywords.

4. Printing Elements of Jagged Array

Moving on, we can print the elements of a jagged array to the console by using a for loop to iterate over each row and its elements:

for (int i = 0; i < multiDimensionalArray.length; i++) {
    for (int j = 0; j < multiDimensionalArray[i].length; j++) {
        System.out.print(multiDimensionalArray[i][j] + " ");
    }
    System.out.println();
}

In the code above, we loop through each row of the array and print its elements. Here’s the output:

1 2 
3 4 5 
6 7 8 9 

Alternatively, we can print each row using the Arrays.toString() method:

for (int index = 0; index < multiDimensionalArray.length; index++) {
    System.out.println(Arrays.toString(multiDimensionalArray[index]));
}

This approach avoids using the inner loop, and it prints each row as a formatted string:

[1, 2] [3, 4, 5] [6, 7, 8, 9]

5. Length of Elements

We can find the length of the arrays in a multi-dimensional array by iterating over the main array:

int[] findLengthOfElements(int[][] multiDimensionalArray) {
    int[] arrayOfLengths = new int[multiDimensionalArray.length];
    for (int i = 0; i < multiDimensionalArray.length; i++) {
        arrayOfLengths[i] = multiDimensionalArray[i].length;
    }
    return arrayOfLengths;
}

We can also find the length of arrays using streams:

Integer[] findLengthOfArrays(int[][] multiDimensionalArray) {
    return Arrays.stream(multiDimensionalArray)
      .map(array -> array.length)
      .toArray(Integer[]::new);
}

Here, we use map() to extract the length and collect them into an Integer[] array.

6. Copy a 2D Array

We can copy a 2D array using the Arrays.copyOf() method:

int[][] copy2DArray(int[][] arrayOfArrays) {
    int[][] copied2DArray = new int[arrayOfArrays.length][];
    for (int i = 0; i < arrayOfArrays.length; i++) {
        int[] array = arrayOfArrays[i];
        copied2DArray[i] = Arrays.copyOf(array, array.length);
    }
    return copied2DArray;
}

We can also achieve this by using streams:

Integer[][] copy2DArray(Integer[][] arrayOfArrays) {
    return Arrays.stream(arrayOfArrays)
      .map(array -> Arrays.copyOf(array, array.length))
      .toArray(Integer[][]::new);
}

The stream version performs the same deep copy operation in a functional style.

7. Conclusion

In this article, we looked at what jagged arrays are, how they look in memory, and how we can define, initialize, and use them.

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

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.

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