Haha, been there. I started using Java back when Java 8 was hot—and even then, I wished for java operator overloading at times. But if you’re using Java 14 or later, there’s something that makes this even smoother: Records.
Records are immutable and get rid of all the boilerplate. Combine that with method chaining and you’ve got a modern, clean setup:
record Complex(double real, double imag) {
Complex add(Complex other) {
return new Complex(this.real + other.real, this.imag + other.imag);
}
}
// Usage
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
Complex c = a.add(b);
System.out.println(c); // Output: 4.0 + 6.0i
It’s still not a + b, but now your code’s concise, readable, and immutable—all good things. Honestly, once you embrace how Java does things, you stop missing java operator overloading as much.