<p>With the exception of <code>java.lang.String</code> being treated as a special case<sup>1</sup>, Java does not allow you to <em>define</em> the behaviour of <code>+</code> 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 <em>operator overloading</em>.</p>

<p>Your best bet is to build functions like <code>add</code> &amp;c. Appeal to precedent here: see how the Java guys have done it with <code>BigInteger</code>, for example. Sadly there is no way of defining the <em>precedence</em> 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 mess<sup>2</sup>.</p>

<hr>

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

<p><sup>2</sup> 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 <a href="https://arxiv.org/pdf/1502.03808v1.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1502.03808v1.pdf</a></p>
How can I add 2 objects in java
<p>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.</p>

<p>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".</p>

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