1

I think that there is something like this in C/C++, but what about in java? Can I add a method that will add two of them together, but instead of doing say obj1.add(obj2) you can do obj1 + obj2? Can you do the same for -, *, /, and == & other comparatives? Can you create a primitive, or would that take modifying the JVM?

user2507230
  • 562
  • 1
  • 3
  • 12

2 Answers2

1

That's called operator overloading. And no Java does not support it. You can find more information here.

Community
  • 1
  • 1
rocketboy
  • 9,573
  • 2
  • 34
  • 36
0

No you cannot. There is no operator overloading in Java. The only thing that comes close to an overloaded operator in Java is + which can concatenate strings and perform standard addition.

Kon
  • 10,702
  • 6
  • 41
  • 58