10

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).

Community
  • 1
  • 1
ahodder
  • 11,353
  • 14
  • 71
  • 114

6 Answers6

16

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)
DNA
  • 42,007
  • 12
  • 107
  • 146
10

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.

Bruno
  • 119,590
  • 31
  • 270
  • 376
  • That said, Java not having operator overloading was a very deliberate decision -- you may find it best to avoid such things. – Louis Wasserman Apr 23 '12 at 22:51
  • 3
    @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. – Bruno Apr 23 '12 at 23:06
  • Dead link? It doesn't load for me. – Aaron Franke Apr 11 '18 at 01:05
0

No, you can't overload special symbols for operators in Java.

Riley Lark
  • 20,660
  • 15
  • 80
  • 128
0

As all others have said, you absolutely cannot add new operators in Java. However, other JVM languages that are friendly to Java -- like Groovy -- will let you define new operators from existing operator tokens.

Kevin Welker
  • 7,719
  • 1
  • 40
  • 56
0

No. Read this article for an argument of why they shouldn't be: http://java.dzone.com/articles/why-java-doesnt-need-operator

You could use a different language, like Scala, to achieve this on the java platform. - https://stackoverflow.com/a/1991348/1342121

Community
  • 1
  • 1
ianpojman
  • 1,743
  • 1
  • 15
  • 20
  • 4
    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)`](http://commons.apache.org/math/apidocs/org/apache/commons/math3/linear/AbstractRealMatrix.html#multiply%28org.apache.commons.math3.linear.RealMatrix%29) everywhere. – Bruno Apr 23 '12 at 20:58
  • 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 – ianpojman Apr 26 '12 at 19:34
0

No. Java doesn't allow operator overloading. You can see more discussion here: https://javarevisited.blogspot.com/2011/08/why-java-does-not-support-operator.html

logbasex
  • 1,688
  • 1
  • 16
  • 22
  • 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](/review/low-quality-posts/25294228) – David Buck Feb 07 '20 at 10:46
  • @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. – Mark Rotteveel Feb 07 '20 at 10:48
  • @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. – logbasex Feb 08 '20 at 18:03