Is it possible to create a custom operator in Java?

13,609

Solution 1

Java doesn't allow for this.

However, if you want to achieve this sort of syntax whilst being able to run your code on a JVM (and with other Java code), you could look at Groovy, which has operator overloading (and with which you could also use DSLs for short syntax which would have similar effects to using custom operators).

Note that defining custom operators (not just overloading) is a big deal in any language, since you would have to be able to alter the lexer and grammar somehow.

Solution 2

No, Java is not extensible in this way. You can't add operators, and you can't even further overload built-in operators like + - even standard library classes like BigInteger have to use methods such as add() rather than operators such as +.

Scala (another static JVM language) gets around this by using method calls rather than built-in operators, and allowing any characters in method names, so you can define new methods that appear to be operators, i.e.

a + 1

is syntactic sugar for:

a.+(1)
Share:
13,609
ahodder
Author by

ahodder

I'm a derp who derps with herps in hopes that the herp does all the derping I want it to.

Updated on June 04, 2022

Comments

  • ahodder
    ahodder almost 2 years

    Similar to Is it possible to create a new operator in c#?, is it possible to create your own operator for Java? I would initially say no since you can't overload, but then again, String supports + and += (implicitly through StringBuilder at execution time etc).

  • ahodder
    ahodder almost 12 years
    I'm not asking to overload. I'm asking to create a new symbol.
  • Kevin Welker
    Kevin Welker almost 12 years
    in your question, you used both terms "new" and "overload" so it is a bit confusing.
  • Bruno
    Bruno almost 12 years
    That article was visibly written by someone who hasn't done any numerical code in Java. Being able to multiply matrices with operator overloading with NumPy in Python (for example) is very handy for readability compared with having to call the likes of RealMatrix.multiply(RealMatrix) everywhere.
  • Louis Wasserman
    Louis Wasserman almost 12 years
    That said, Java not having operator overloading was a very deliberate decision -- you may find it best to avoid such things.
  • Bruno
    Bruno almost 12 years
    @LouisWasserman: as I was saying in another comment, I understand the decision, but that doesn't make Java very suitable for math programming unfortunately. With matrices, a.multiply(b.multiply(c.add(d)).add(e)) isn't great... NumPy makes good usage of operator overloading in Python.
  • ianpojman
    ianpojman almost 12 years
    agreed... I haven't done much of it at all, and the Java language doesn't seem like the best choice for that. The Java platform is great for it though
  • Aaron Franke
    Aaron Franke about 6 years
    Dead link? It doesn't load for me.
  • David Buck
    David Buck about 4 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • Mark Rotteveel
    Mark Rotteveel about 4 years
    @DavidBuck the answer is "No. Java doesn't allow operator overloading.", the link just supports that claim. In other words, this is not a link-only answer.
  • logbasex
    logbasex about 4 years
    @DavidBuck, MarkRotteveel: I'm initially intended to write a comment but my reputation wasn't enough. Thank you for all kindness, I'll better at the next time as a contributor.