There is no operator overload in Java.
For example, BigDecimal will be much more popular if you can write a + b instead of a.add(b) .
Method 1
Imaginary i3 = i1.add(i2);
Method:
public static Imaginary add(Imaginary i2) { return new Imaginary(real + i2.real, imaginary + i2.imaginary); }
Way 2 .
Imaginary i3 = add(i1, i2)
Method:
public static Imaginary add(Imaginary i1, Imaginary i2) { return new Imaginary(i1.real + i2.real, i1.imaginary + i2.imaginary); }
Overloading the operator would definitely make the design more complex than without it, and this could lead to a more complex compiler or slowdown of the JVM.
loknath
source share