How to Store X and Y Coordinates in Java
When building applications in Java that deal with two-dimensional data, one of the most common needs is to represent and store X and Y coordinates. These coordinates may represent points on a graph, pixel positions in a user interface, or nodes in a simulation. There are multiple ways to model this data in Java, each with its strengths and weaknesses. In this article, we will compare records, POJOs (Plain Old Java Objects), arrays, and the built-in class as potential solutions.java.awt.geom.Point2D.Double
1. Using Records
Java records provide a concise and immutable way of modeling data. A record automatically generates constructors, equals(), hashCode(), and toString() methods, making it well-suited for small, immutable data carriers such as coordinates.
public record Coordinate(Double x, Double y) {}
public class RecordExample {
public static void main(String[] args) {
Coordinate point = new Coordinate(10.5, 20.75);
System.out.println("X: " + point.x());
System.out.println("Y: " + point.y());
System.out.println("Point: " + point);
}
}
Here, the Coordinate record holds two Double fields: x and y. Using Double allows us to represent fractional coordinates like (10.5, 20.75). The record remains immutable, ensuring data integrity once created.
2. Using POJOs
A Plain Old Java Object (POJO) is the traditional approach for modeling data in Java. With a POJO, developers define fields, constructors, getters, setters, and optionally override toString(). This approach provides flexibility and mutability, which can be useful when coordinates need frequent updates.
public class PojoExample {
// Defining a POJO for coordinates
public static class Coordinate {
private Double x;
private Double y;
public Coordinate(Double x, Double y) {
this.x = x;
this.y = y;
}
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
@Override
public String toString() {
return "Coordinate{x=" + x + ", y=" + y + "}";
}
}
public static void main(String[] args) {
Coordinate point = new Coordinate(10.5, 20.75);
System.out.println("Initial: " + point);
// Mutating the coordinate values
point.setX(15.25);
point.setY(25.5);
System.out.println("Updated: " + point);
}
}
This POJO allows mutable Double values for coordinates. Unlike the record, which is immutable, the POJO can easily change its internal state. This is useful in scenarios where objects move or update positions dynamically, such as in animations or physics simulations.
3. Using Arrays
A lightweight alternative is to use arrays to store coordinates. This avoids creating custom classes but sacrifices readability and type safety. Typically, an array of length 2 represents an (x, y) coordinate.
public class ArrayExample {
public static void main(String[] args) {
Double[] coordinate = new Double[2];
coordinate[0] = 10.5; // x
coordinate[1] = 20.75; // y
System.out.println("X: " + coordinate[0]);
System.out.println("Y: " + coordinate[1]);
}
}
This example uses a Double[] array to represent coordinates. While simple and lightweight, it relies on convention (coordinate[0] = x, coordinate[1] = y) which reduces readability. Additionally, arrays lack convenience methods and can be error-prone in larger systems.
4. Using java.awt.geom.Point2D.Double
Instead of java.awt.Point (which uses integers), Java also provides java.awt.geom.Point2D.Double, which directly supports double-precision coordinates. This class is mutable and includes methods for movement, distance calculation, and geometric operations.
public class Point2DExample {
public static void main(String[] args) {
Point2D.Double point = new Point2D.Double(10.5, 20.75);
System.out.println("X: " + point.x);
System.out.println("Y: " + point.y);
System.out.println("Point: " + point);
// Moving the point
point.setLocation(15.25, 25.5);
System.out.println("Moved Point: " + point);
// Calculating distance
Point2D.Double another = new Point2D.Double(20.0, 30.0);
double distance = point.distance(another);
System.out.println("Distance to another point: " + distance);
}
}
Here, Point2D.Double provides a robust, built-in way to work with double-precision coordinates. Unlike arrays or POJOs, it comes with geometric methods out of the box, making it especially useful in GUI, graphics, or scientific applications.
5. Tradeoffs and Comparison
- Records: Immutable, concise, type-safe. Great for simple data carriers, but immutability can be restrictive for dynamic coordinates.
- POJOs: Flexible and mutable. Useful for objects that need frequent updates, but verbose compared to records.
- Arrays: Lightweight and minimal, but readability and type safety are poor. Better suited for quick or performance-critical tasks.
Point2D.Double: Feature-rich and built-in, ideal for GUI, geometry, and scientific use cases, but tied to the AWT/Java2D ecosystem.
6. Conclusion
In this article, we explored four common ways to store X and Y coordinates in Java: using records, POJOs, arrays, and the built-in Point2D.Double class. Records offer immutability and reduced boilerplate, POJOs provide flexibility and mutability, arrays are compact but less readable, and Point brings convenience methods at the cost of GUI dependency. The best choice depends on the context: use records for immutable data, POJOs for mutable models, arrays for lightweight or performance-sensitive scenarios, and Point when you want built-in functionality with minimal coding effort.
7. Download the Source Code
This article explored different ways to store X and Y coordinates in Java.
You can download the full source code of this example here: java store x y coordinates

