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

From plotting points on a map to capturing the exact location where a user clicks their mouse, we often need to store (x, y) coordinates.

Java doesn’t have a coordinates construct that’s in common use, although storing coordinates is not that uncommon. In this tutorial, we’ll compare records, POJOs, and arrays as potential ways to store these coordinates and explore the tradeoffs of each.

2. Using a Point Record

Records in Java are designed to be simple data carriers.  Furthermore, they’re immutable and automatically generate constructors, getters, equals(), hashCode(), and toString() methods, significantly reducing boilerplate code.

Thus, records are ideal for representing simple data aggregates, such as (x, y) coordinate pairs.

2.1. Defining a Record

This one line creates the record with all necessary methods:

public record PointRecord(double x, double y) {}

We declare each coordinate as a double because it can hold a decimal value, whereas an int cannot.

2.2. Example Record

Let’s verify that a given PointRecord instance stores the (x, y) coordinates correctly:

@Test
void givenAPointRecord_whenUsingAccessorMethods_thenRecordReturnsCoordinatesCorrectly() {
    PointRecord point = new PointRecord(30.5, 40.0);

    assertEquals(30.5, point.x(), "X coordinate should be 30.5");
    assertEquals(40.0, point.y(), "Y coordinate should be 40.0");
}

In this example, we create a PointRecord instance with coordinates (30.5, 40.0). Then, we test the assertion that its accessor methods return the correct coordinates.

Let’s verify that PointRecords with the same coordinates are equal:

@Test
void givenTwoRecordsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() {
    PointRecord point1 = new PointRecord(7.0, 8.5);
    PointRecord point2 = new PointRecord(7.0, 8.5);

    assertEquals(point1, point2, "Records with same coordinates should be equal");
}

3. Using a Point POJO

A POJO is a standard Java class that encapsulates data. This approach is flexible and widely understood as it’s a classic object-oriented solution.

3.1. Defining a Point Class

We define private fields for the coordinates and public methods (getters) to access them:

public class PointPojo {
    private final double x;
    private final double y;

    public PointPojo(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // standard setters and getters

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PointPojo point = (PointPojo) o;
        return Double.compare(point.x, x) == 0 &&
          Double.compare(point.y, y) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}

3.2. Example Point

Let’s verify that a given PointPojo instance stores the (x, y) coordinates correctly:

@Test
void givenAPoint_whenUsingGetter_thenPointReturnsCoordinatesCorrectly() {
    PointPojo point = new PointPojo(10.0, 20.5);

    assertEquals(10.0, point.getX(), "X coordinate should be 10.0");
    assertEquals(20.5, point.getY(), "Y coordinate should be 20.5");
}

In this example, we create a PointPojo instance with coordinates (10.0, 20.5). Afterward, we test the assertion that its getters return the correct coordinates. Additionally, we verify that PointPojos with the same coordinates are equal:

@Test
void givenTwoPointsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() {
    PointPojo point1 = new PointPojo(5.1, -3.5);
    PointPojo point2 = new PointPojo(5.1, -3.5);
        
    assertEquals(point1, point2, "Points with same coordinates should be equal");
}

4. Using java.awt

Java’s Abstract Window Toolkit (AWT) library includes a class specifically for this purpose. The AWT library is especially useful in GUI applications.

AWT has a class called java.awt.Point; however, it uses only integers. The java.awt.Point2D.Double class offers double precision.

Unless we are building an AWT-based app, though, we should favor other solutions.

5. Using an Array

The most minimalistic approach is to use a double array of two elements. By convention, the first element stores the x coordinate, and the second element stores the y coordinate.

To demonstrate, let’s use an array to represent coordinates (15.0, 25.0):

@Test
void givenArrayOfCoordinates_whenAccessingArrayIndices_thenReturnsCoordinatesAtCorrectIndices() {
    double[] coordinates = new double[2];
    coordinates[0] = 15.0; // x
    coordinates[1] = 25.0; // y

    assertEquals(15.0, coordinates[0], "Index 0 should be the X coordinate");
    assertEquals(25.0, coordinates[1], "Index 1 should be the Y coordinate");
}

6. Choosing the Best Method

All four methods may be suitable depending on our use case. Primarily, we need to consider how we want to use the (x, y) coordinates once stored.

Accordingly, if we want to extend our data structure or are using Java 15 or earlier, we should create a POJO. One example of this would be extending our PointPojo class to take on three coordinates (x, y, z). A downside is the extra boilerplate.

Alternatively, we can choose records if we are using Java 16 or later. These are succinct and well-suited to the task.

Further, if our top priority is to be fast, we can sacrifice encapsulation and readability for performance by using a 2-cell double array. However, we need to be aware of how we implement an array since nothing enforces that the array must have exactly two elements or which index corresponds to which coordinate.

7. Conclusion

In this article, we explored four ways of storing a pair of (x, y) coordinates representing a point in Java in the Cartesian coordinate system.

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)