-2

I am programming a vector class (in the mathematical sense), and it would be nice if I could do

vector1 + vector2

instead of

vector1.add(vector2)

Is this possible in java? Can I assign a behaviour to an arithmetic operation like the String class is able to? Or is this hardcoded in the compiler?

Lucas Kuhlemann
  • 169
  • 2
  • 18

2 Answers2

0

Operator overloading is used in JAVA only for concatenating String datatypes.

String str = "string1" + "string2";

The designers of Java wanted to keep Java simple and found that operator overloading made code more complex and difficult to read. There are other methods available to achieve the same functionality as operator overloading, you could create methods in a class named plus(), minus(), multiply(), etc...

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

There is no operator overloading in Java.

But we can concatenate string using the operator '+' but that is not constructor overloading.

String str="A"+"B"; is not operator overloading its just concatenation.

Other customized overloading is not permitted in java.