3

I'm new to Java and I couldn't find an answer to it anywhere because i don't even know how to search for it.

I want to define how 2 objects can be added together, so you get a new one like for example you can add String "a" and String "b" to get "ab".

I know this can be done in python by doing self.__add__(self, other). How can you do this in Java?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
jamie97
  • 41
  • 2
  • In case of strings you can just use +: `"a" + "b"`. For all other objects (except wrapper classes for primitive number types) you need to provide a method in the class. As an example, have a look at `BigDecimal`. – Thomas Apr 21 '17 at 10:58
  • 2
    No, Java doesn't support user-defined operator overloading http://stackoverflow.com/questions/1686699/operator-overloading-in-java – Sidias-Korrado Apr 21 '17 at 10:58
  • Do you want to concatenate Strings? Then you can use the `+` operator. If you want to overload `+`, that's not possible. Otherwise you can just create a method (maybe call it `add`) – SilverNak Apr 21 '17 at 10:59

3 Answers3

2

The thing you are looking for is called operator overloading. It exists in some languages, however in Java it does not.

The best thing you can do is to define a method add() inside the class and then use it like this:

object1.add(object2);

I know it looks nicer with a + between them, but that would make compiling more complex.

krikki13
  • 46
  • 5
  • 4
    We should note that built-in operator overloading does exist in Java, what doesn't exist is the user-defined variety. – biziclop Apr 21 '17 at 11:00
1

With the exception of java.lang.String being treated as a special case1, Java does not allow you to define the behaviour of + for arbitrary types, or indeed any other operator, as you can in some languages such as C++ or Scala. In other words, Java does not support operator overloading.

Your best bet is to build functions like add &c. Appeal to precedent here: see how the Java guys have done it with BigInteger, for example. Sadly there is no way of defining the precedence of your functions, so you have to use very many parentheses to tell the compiler how you want an expression to be evaluated. It's for this reason that I don't use Java for any serious mathematical applications as the implementation of even a simple equation quickly becomes an unreadable mess2.


1 Which in some ways does more harm than good: e.g. consider 1 + 2 + "Hello" + 3 + 4. This compile time constant expression is a string type with the value 3Hello34.

2 Note that C++ was used to model the gravitational lensing effects of the wormhole in the movie "Interstellar". I challenge anyone to do that in a language that does not support operator overloading! See https://arxiv.org/pdf/1502.03808v1.pdf

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Why can't I upvote your answer? It says I need 15 reputation, but how can i get that? – jamie97 Apr 21 '17 at 11:16
  • 1
    Earn it by asking good questions (such as this one: I will upvote it), writing good answers and making good edit suggestions. The 15 rep threshold guards us against trolls. (Good answers are normally upvoted eventually). – Bathsheba Apr 21 '17 at 11:17
  • Thanks for the upvote, but since i am a beginner i cannot really answer many questions out there and definitely not better like someone like you. – jamie97 Apr 21 '17 at 11:20
  • I think it is on the whole. Trolls distorting the voting process could discredit it - it does take a surprisingly large amount of effort to earn +15 on this site! As a beginner you can still ask good questions, and make good edit suggestions. – Bathsheba Apr 21 '17 at 11:22
  • `I challenge anyone to do that in a language that does not support operator overloading!` As so often happens in software development, it's horses for courses. – biziclop Apr 21 '17 at 12:13
0

Java does not allow you to override operators. String is a special case that does allow this functionality.

What you can do is add an add function like so:

public YourObject add(YourObject yourObject){
    return new YourObject(this.propertyToAdd + yourObject.propertyToAdd);
}
CraigR8806
  • 1,584
  • 13
  • 21