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 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.
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 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW