<p>Coming from C++ to Java, the obvious unanswered question is why didn't Java include operator overloading?</p>

<p>Isn't <code>Complex a, b, c; a = b + c;</code> much simpler than <code>Complex a, b, c; a = b.add(c);</code>?</p>

<p>Is there a known reason for this, valid arguments for <em>not</em> allowing operator overloading? Is the reason arbitrary, or lost to time?</p>

[{'answer_id': 77738, 'author': 'Sarien', 'author_id': 1994377, 'author_profile': 'https://Stackoverflow.com/users/1994377', 'pm_score': 3, 'selected': False, 'text': "<p>Well you can really shoot yourself in the foot with operator overloading. It's like with pointers people make stupid mistakes with them and so it was decided to take the scissors away.</p>\n\n<p>At least I think that's the reason.\nI'm on your side anyway. :)</p>\n"}, {'answer_id': 77790, 'author': 'user14128', 'author_id': 14128, 'author_profile': 'https://Stackoverflow.com/users/14128', 'pm_score': 3, 'selected': False, 'text': '<p>I think this may have been a conscious design choice to force developers to create functions whose names clearly communicate their intentions. In C++ developers would overload operators with functionality that would often have no relation to the commonly accepted nature of the given operator, making it nearly impossible to determine what a piece of code does without looking at the definition of the operator.</p>\n'}, {'answer_id': 77798, 'author': 'Sebastian Redl', 'author_id': 8922, 'author_profile': 'https://Stackoverflow.com/users/8922', 'pm_score': 4, 'selected': False, 'text': "<p>The Java designers decided that operator overloading was more trouble than it was worth. Simple as that.</p>\n\n<p>In a language where every object variable is actually a reference, operator overloading gets the additional hazard of being quite illogical - to a C++ programmer at least. Compare the situation with C#'s == operator overloading and <code>Object.Equals</code> and <code>Object.ReferenceEquals</code> (or whatever it's called).</p>\n"}, {'answer_id': 77811, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 2, 'selected': False, 'text': '<p>Assuming Java as the implementation language then a, b, and c would all be references to type Complex with initial values of null. Also assuming that Complex is immutable as the mentioned <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html" rel="nofollow noreferrer">BigInteger</a> and similar immutable <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html" rel="nofollow noreferrer">BigDecimal</a>, I\'d I think you mean the following, as you\'re assigning the reference to the Complex returned from adding b and c, and not comparing this reference to a.</p>\n\n<blockquote>\n  <p>Isn\'t :</p>\n\n<pre><code>Complex a, b, c; a = b + c;\n</code></pre>\n  \n  <p><em>much</em> simpler than:</p>\n\n<pre><code>Complex a, b, c; a = b.add(c);\n</code></pre>\n</blockquote>\n'}, {'answer_id': 77908, 'author': 'noah', 'author_id': 12034, 'author_profile': 'https://Stackoverflow.com/users/12034', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://groovy.codehaus.org/" rel="noreferrer">Groovy</a> has operator overloading, and runs in the JVM. If you don\'t mind the performance hit (which gets smaller everyday). It\'s automatic based on method names. e.g., \'+\' calls the \'plus(argument)\' method.</p>\n'}, {'answer_id': 77963, 'author': 'Aaron', 'author_id': 14153, 'author_profile': 'https://Stackoverflow.com/users/14153', 'pm_score': 5, 'selected': True, 'text': "<p>Assuming you wanted to overwrite the previous value of the object referred to by <code>a</code>, then a member function would have to be invoked.</p>\n\n<pre><code>Complex a, b, c;\n// ...\na = b.add(c);\n</code></pre>\n\n<p>In C++, this expression tells the compiler to create three (3) objects on the stack, perform addition, and <em>copy</em> the resultant value from the temporary object into the existing object <code>a</code>.</p>\n\n<p>However, in Java, <code>operator=</code> doesn't perform value copy for reference types, and users can only create new reference types, not value types. So for a user-defined type named <code>Complex</code>, assignment means to copy a reference to an existing value.</p>\n\n<p>Consider instead:</p>\n\n<pre><code>b.set(1, 0); // initialize to real number '1'\na = b; \nb.set(2, 0);\nassert( !a.equals(b) ); // this assertion will fail\n</code></pre>\n\n<p>In C++, this copies the value, so the comparison will result not-equal. In Java, <code>operator=</code> performs reference copy, so <code>a</code> and <code>b</code> are now referring to the same value. As a result, the comparison will produce 'equal', since the object will compare equal to itself.</p>\n\n<p>The difference between copies and references only adds to the confusion of operator overloading. As @Sebastian mentioned, Java and C# both have to deal with value and reference equality separately -- <code>operator+</code> would likely deal with values and objects, but <code>operator=</code> is already implemented to deal with references.</p>\n\n<p>In C++, you should only be dealing with one kind of comparison at a time, so it can be less confusing. For example, on <code>Complex</code>, <code>operator=</code> and <code>operator==</code> are both working on values -- copying values and comparing values respectively.</p>\n"}, {'answer_id': 78086, 'author': 'Garth Gilmour', 'author_id': 2635682, 'author_profile': 'https://Stackoverflow.com/users/2635682', 'pm_score': 5, 'selected': False, 'text': '<p>James Gosling likened designing Java to the following:</p>\n\n<blockquote>\n  <p>"There\'s this principle about moving, when you move from one apartment to another apartment. An interesting experiment is to pack up your apartment and put everything in boxes, then move into the next apartment and not unpack anything until you need it. So you\'re making your first meal, and you\'re pulling something out of a box. Then after a month or so you\'ve used that to pretty much figure out what things in your life you actually need, and then you take the rest of the stuff -- forget how much you like it or how cool it is -- and you just throw it away. It\'s amazing how that simplifies your life, and you can use that principle in all kinds of design issues: not do things just because they\'re cool or just because they\'re interesting."</p>\n</blockquote>\n\n<p>You can read the <a href="http://www.gotw.ca/publications/c_family_interview.htm" rel="noreferrer">context of the quote here</a></p>\n\n<p>Basically operator overloading is great for a class that models some kind of point, currency or complex number. But after that you start running out of examples fast.</p>\n\n<p>Another factor was the abuse of the feature in C++ by developers overloading operators like \'&amp;&amp;\', \'||\', the cast operators and of course \'new\'. The complexity resulting from combining this with pass by value and exceptions is well covered in the <a href="https://rads.stackoverflow.com/amzn/click/com/0201615622" rel="noreferrer" rel="nofollow noreferrer">Exceptional C++</a> book.</p>\n'}, {'answer_id': 78167, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>Sometimes it would be nice to have operator overloading, friend classes and multiple inheritance.</p>\n\n<p>However I still think it was a good decision. If Java would have had operator overloading then we could never be sure of operator meanings without looking through source code. At present that's not necessary. And I think your example of using methods instead of operator overloading is also quite readable. If you want to make things more clear you could always add a comment above hairy statements.</p>\n\n<pre><code>// a = b + c\nComplex a, b, c; a = b.add(c);\n</code></pre>\n"}, {'answer_id': 82890, 'author': 'user15793', 'author_id': 15793, 'author_profile': 'https://Stackoverflow.com/users/15793', 'pm_score': 5, 'selected': False, 'text': '<p>Check out Boost.Units: <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/boost_units.html" rel="noreferrer">link text</a></p>\n\n<p>It provides zero-overhead Dimensional analysis through operator overloading. How much clearer can this get?</p>\n\n<pre><code>quantity&lt;force&gt;     F = 2.0*newton;\nquantity&lt;length&gt;    dx = 2.0*meter;\nquantity&lt;energy&gt;    E = F * dx;\nstd::cout &lt;&lt; "Energy = " &lt;&lt; E &lt;&lt; endl;\n</code></pre>\n\n<p>would actually output "Energy = 4 J" which is correct.</p>\n'}, {'answer_id': 194889, 'author': 'paercebal', 'author_id': 14089, 'author_profile': 'https://Stackoverflow.com/users/14089', 'pm_score': 10, 'selected': False, 'text': '<p>There are a lot of posts complaining about operator overloading.</p>\n<p>I felt I had to clarify the &quot;operator overloading&quot; concepts, offering an alternative viewpoint on this concept.</p>\n<h1>Code obfuscating?</h1>\n<p>This argument is a fallacy.</p>\n<h2>Obfuscating is possible in all languages...</h2>\n<p>It is as easy to obfuscate code in C or Java through functions/methods as it is in C++ through operator overloads:</p>\n<pre><code>// C++\nT operator + (const T &amp; a, const T &amp; b) // add ?\n{\n   T c ;\n   c.value = a.value - b.value ; // subtract !!!\n   return c ;\n}\n\n// Java\nstatic T add (T a, T b) // add ?\n{\n   T c = new T() ;\n   c.value = a.value - b.value ; // subtract !!!\n   return c ;\n}\n\n/* C */\nT add (T a, T b) /* add ? */\n{\n   T c ;\n   c.value = a.value - b.value ; /* subtract !!! */\n   return c ;\n}\n</code></pre>\n<h2>...Even in Java\'s standard interfaces</h2>\n<p>For another example, let\'s see the <a href="http://download.oracle.com/javase/7/docs/api/java/lang/Cloneable.html" rel="noreferrer"><code>Cloneable</code> interface</a> in Java:</p>\n<p>You are supposed to clone the object implementing this interface. But you could lie. And create a different object. In fact, this interface is so weak you could return another type of object altogether, just for the fun of it:</p>\n<pre><code>class MySincereHandShake implements Cloneable\n{\n    public Object clone()\n    {\n       return new MyVengefulKickInYourHead() ;\n    }\n}\n</code></pre>\n<p>As the <code>Cloneable</code> interface can be abused/obfuscated, should it be banned on the same grounds C++ operator overloading is supposed to be?</p>\n<p>We could overload the <code>toString()</code> method of a <code>MyComplexNumber</code> class to have it return the stringified hour of the day. Should the <code>toString()</code> overloading be banned, too? We could sabotage <code>MyComplexNumber.equals</code> to have it return a random value, modify the operands... etc. etc. etc..</p>\n<p><b>In Java, as in C++, or whatever language, the programmer must respect a minimum of semantics when writing code. This means implementing an <code>add</code> function that adds, and <code>Cloneable</code> implementation method that clones, and a <code>++</code> operator that increments.</b></p>\n<h1>What\'s obfuscating anyway?</h1>\n<p>Now that we know that code can be sabotaged even through the pristine Java methods, we can ask ourselves about the real use of operator overloading in C++?</p>\n<h2>Clear and natural notation: methods vs. operator overloading?</h2>\n<p>We\'ll compare below, for different cases, the &quot;same&quot; code in Java and C++, to have an idea of which kind of coding style is clearer.</p>\n<h3>Natural comparisons:</h3>\n<pre><code>// C++ comparison for built-ins and user-defined types\nbool    isEqual          = A == B ;\nbool    isNotEqual       = A != B ;\nbool    isLesser         = A &lt;  B ;\nbool    isLesserOrEqual  = A &lt;= B ;\n\n// Java comparison for user-defined types\nboolean isEqual          = A.equals(B) ;\nboolean isNotEqual       = ! A.equals(B) ;\nboolean isLesser         = A.comparesTo(B) &lt; 0 ;\nboolean isLesserOrEqual  = A.comparesTo(B) &lt;= 0 ;\n</code></pre>\n<p>Please note that A and B could be of any type in C++, as long as the operator overloads are provided. In Java, when A and B are not primitives, the code can become very confusing, even for primitive-like objects (BigInteger, etc.)...</p>\n<h3>Natural array/container accessors and subscripting:</h3>\n<pre><code>// C++ container accessors, more natural\nvalue        = myArray[25] ;         // subscript operator\nvalue        = myVector[25] ;        // subscript operator\nvalue        = myString[25] ;        // subscript operator\nvalue        = myMap[&quot;25&quot;] ;         // subscript operator\nmyArray[25]  = value ;               // subscript operator\nmyVector[25] = value ;               // subscript operator\nmyString[25] = value ;               // subscript operator\nmyMap[&quot;25&quot;]  = value ;               // subscript operator\n\n// Java container accessors, each one has its special notation\nvalue        = myArray[25] ;         // subscript operator\nvalue        = myVector.get(25) ;    // method get\nvalue        = myString.charAt(25) ; // method charAt\nvalue        = myMap.get(&quot;25&quot;) ;     // method get\nmyArray[25]  = value ;               // subscript operator\nmyVector.set(25, value) ;            // method set\nmyMap.put(&quot;25&quot;, value) ;             // method put\n</code></pre>\n<p>In Java, we see that for each container to do the same thing (access its content through an index or identifier), we have a different way to do it, which is confusing.</p>\n<p>In C++, each container uses the same way to access its content, thanks to operator overloading.</p>\n<h3>Natural advanced types manipulation</h3>\n<p>The examples below use a <code>Matrix</code> object, found using the first links found on Google for &quot;<a href="https://encrypted.google.com/search?q=Java+Matrix+object" rel="noreferrer">Java Matrix object</a>&quot; and &quot;<a href="https://encrypted.google.com/search?q=c%2B%2B+Matrix+object" rel="noreferrer">C++ Matrix object</a>&quot;:</p>\n<pre><code>// C++ YMatrix matrix implementation on CodeProject\n// http://www.codeproject.com/KB/architecture/ymatrix.aspx\n// A, B, C, D, E, F are Matrix objects;\nE =  A * (B / 2) ;\nE += (A - B) * (C + D) ;\nF =  E ;                  // deep copy of the matrix\n\n// Java JAMA matrix implementation (seriously...)\n// http://math.nist.gov/javanumerics/jama/doc/\n// A, B, C, D, E, F are Matrix objects;\nE = A.times(B.times(0.5)) ;\nE.plusEquals(A.minus(B).times(C.plus(D))) ;\nF = E.copy() ;            // deep copy of the matrix\n</code></pre>\n<p>And this is not limited to matrices. The <code>BigInteger</code> and <code>BigDecimal</code> classes of Java suffer from the same confusing verbosity, whereas their equivalents in C++ are as clear as built-in types.</p>\n<h3>Natural iterators:</h3>\n<pre><code>// C++ Random Access iterators\n++it ;                  // move to the next item\n--it ;                  // move to the previous item\nit += 5 ;               // move to the next 5th item (random access)\nvalue = *it ;           // gets the value of the current item\n*it = 3.1415 ;          // sets the value 3.1415 to the current item\n(*it).foo() ;           // call method foo() of the current item\n\n// Java ListIterator&lt;E&gt; &quot;bi-directional&quot; iterators\nvalue = it.next() ;     // move to the next item &amp; return the value\nvalue = it.previous() ; // move to the previous item &amp; return the value\nit.set(3.1415) ;        // sets the value 3.1415 to the current item\n</code></pre>\n<h3>Natural functors:</h3>\n<pre><code>// C++ Functors\nmyFunctorObject(&quot;Hello World&quot;, 42) ;\n\n// Java Functors ???\nmyFunctorObject.execute(&quot;Hello World&quot;, 42) ;\n</code></pre>\n<h3>Text concatenation:</h3>\n<pre><code>// C++ stream handling (with the &lt;&lt; operator)\n                    stringStream   &lt;&lt; &quot;Hello &quot; &lt;&lt; 25 &lt;&lt; &quot; World&quot; ;\n                    fileStream     &lt;&lt; &quot;Hello &quot; &lt;&lt; 25 &lt;&lt; &quot; World&quot; ;\n                    outputStream   &lt;&lt; &quot;Hello &quot; &lt;&lt; 25 &lt;&lt; &quot; World&quot; ;\n                    networkStream  &lt;&lt; &quot;Hello &quot; &lt;&lt; 25 &lt;&lt; &quot; World&quot; ;\nanythingThatOverloadsShiftOperator &lt;&lt; &quot;Hello &quot; &lt;&lt; 25 &lt;&lt; &quot; World&quot; ;\n\n// Java concatenation\nmyStringBuffer.append(&quot;Hello &quot;).append(25).append(&quot; World&quot;) ;\n</code></pre>\n<p>Ok, in Java you can use <code>MyString = &quot;Hello &quot; + 25 + &quot; World&quot; ;</code> too... But, wait a second: This is operator overloading, isn\'t it? Isn\'t it cheating???</p>\n<p>:-D</p>\n<h2>Generic code?</h2>\n<p>The same generic code modifying operands should be usable both for built-ins/primitives (which have no interfaces in Java), standard objects (which could not have the right interface), and user-defined objects.</p>\n<p>For example, calculating the average value of two values of arbitrary types:</p>\n<pre><code>// C++ primitive/advanced types\ntemplate&lt;typename T&gt;\nT getAverage(const T &amp; p_lhs, const T &amp; p_rhs)\n{\n   return (p_lhs + p_rhs) / 2 ;\n}\n\nint     intValue     = getAverage(25, 42) ;\ndouble  doubleValue  = getAverage(25.25, 42.42) ;\ncomplex complexValue = getAverage(cA, cB) ; // cA, cB are complex\nMatrix  matrixValue  = getAverage(mA, mB) ; // mA, mB are Matrix\n\n// Java primitive/advanced types\n// It won\'t really work in Java, even with generics. Sorry.\n</code></pre>\n<h1>Discussing operator overloading</h1>\n<p>Now that we have seen fair comparisons between C++ code using operator overloading, and the same code in Java, we can now discuss &quot;operator overloading&quot; as a concept.</p>\n<h2>Operator overloading existed since before computers</h2>\n<p><b>Even outside of computer science, there is operator overloading: For example, in mathematics, operators like <code>+</code>, <code>-</code>, <code>*</code>, etc. are overloaded.</b></p>\n<p>Indeed, the signification of <code>+</code>, <code>-</code>, <code>*</code>, etc. changes depending on the types of the operands (numerics, vectors, quantum wave functions, matrices, etc.).</p>\n<p>Most of us, as part of our science courses, learned multiple significations for operators, depending on the types of the operands. Did we find them confusing, then?</p>\n<h2>Operator overloading depends on its operands</h2>\n<p>This is the most important part of operator overloading: Like in mathematics, or in physics, the operation depends on its operands\' types.</p>\n<p>So, know the type of the operand, and you will know the effect of the operation.</p>\n<h2>Even C and Java have (hard-coded) operator overloading</h2>\n<p>In C, the real behavior of an operator will change according to its operands. For example, adding two integers is different than adding two doubles, or even one integer and one double. There is even the whole pointer arithmetic domain (without casting, you can add to a pointer an integer, but you cannot add two pointers...).</p>\n<p>In Java, there is no pointer arithmetic, but someone still found string concatenation without the <code>+</code> operator would be ridiculous enough to justify an exception in the &quot;operator overloading is evil&quot; creed.</p>\n<p>It\'s just that you, as a C (for historical reasons) or Java (for <i>personal reasons</i>, see below) coder, you can\'t provide your own.</p>\n<h2>In C++, operator overloading is not optional...</h2>\n<p>In C++, operator overloading for built-in types is not possible (and this is a good thing), but <i>user-defined</i> types can have <i>user-defined</i> operator overloads.</p>\n<p>As already said earlier, in C++, and to the contrary to Java, user-types are not considered second-class citizens of the language, when compared to built-in types. So, if built-in types have operators, user types should be able to have them, too.</p>\n<p>The truth is that, like the <code>toString()</code>, <code>clone()</code>, <code>equals()</code> methods are for Java (<i>i.e. quasi-standard-like</i>), C++ operator overloading is so much part of C++ that it becomes as natural as the original C operators, or the before mentioned Java methods.</p>\n<p>Combined with template programming, operator overloading becomes a well known design pattern. In fact, you cannot go very far in STL without using overloaded operators, and overloading operators for your own class.</p>\n<h2>...but it should not be abused</h2>\n<p>Operator overloading should strive to respect the semantics of the operator. Do not subtract in a <code>+</code> operator (as in &quot;do not subtract in a <code>add</code> function&quot;, or &quot;return crap in a <code>clone</code> method&quot;).</p>\n<p>Cast overloading can be very dangerous because they can lead to ambiguities. So they should really be reserved for well defined cases. As for <code>&amp;&amp;</code> and <code>||</code>, do not ever overload them unless you really know what you\'re doing, as you\'ll lose the the short circuit evaluation that the native operators <code>&amp;&amp;</code> and <code>||</code> enjoy.</p>\n<h1>So... Ok... Then why it is not possible in Java?</h1>\n<p>Because James Gosling said so:</p>\n<blockquote>\n<p>I left out operator overloading as a <b>fairly personal choice</b> because I had seen too many people abuse it in C++.</p>\n<p><i>James Gosling. Source: <a href="http://www.gotw.ca/publications/c_family_interview.htm" rel="noreferrer">http://www.gotw.ca/publications/c_family_interview.htm</a></i></p>\n</blockquote>\n<p>Please compare Gosling\'s text above with Stroustrup\'s below:</p>\n<blockquote>\n<p>Many C++ design decisions have their roots in my dislike for forcing people to do things in some particular way [...] Often, I was tempted to outlaw a feature I personally disliked, I refrained from doing so because <b>I did not think I had the right to force my views on others</b>.</p>\n<p><i>Bjarne Stroustrup. Source: The Design and Evolution of C++ (1.3 General Background)</i></p>\n</blockquote>\n<h2>Would operator overloading benefit Java?</h2>\n<p>Some objects would greatly benefit from operator overloading (concrete or numerical types, like BigDecimal, complex numbers, matrices, containers, iterators, comparators, parsers etc.).</p>\n<p>In C++, you can profit from this benefit because of Stroustrup\'s humility. In Java, you\'re simply screwed because of Gosling\'s <i>personal choice</i>.</p>\n<h2>Could it be added to Java?</h2>\n<p>The reasons for not adding operator overloading now in Java could be a mix of internal politics, allergy to the feature, distrust of developers (you know, the saboteur ones that seem to haunt Java teams...), compatibility with the previous JVMs, time to write a correct specification, etc..</p>\n<p>So don\'t hold your breath waiting for this feature...</p>\n<h2>But they do it in C#!!!</h2>\n<p>Yeah...</p>\n<p>While this is far from being the only difference between the two languages, this one never fails to amuse me.</p>\n<p>Apparently, the C# folks, with their <i>&quot;every primitive is a <code>struct</code>, and a <code>struct</code> derives from Object&quot;</i>, got it right at first try.</p>\n<h2>And they do it in <a href="https://en.wikipedia.org/wiki/Operator_overloading" rel="noreferrer">other languages</a>!!!</h2>\n<p>Despite all the FUD against used defined operator overloading, the following languages support it: <a href="https://kotlinlang.org/docs/reference/operator-overloading.html" rel="noreferrer">Kotlin</a>, <a href="https://stackoverflow.com/q/1991240">Scala</a>, <a href="https://www.dartlang.org/articles/idiomatic-dart/#operators" rel="noreferrer">Dart</a>, <a href="https://docs.python.org/3/reference/datamodel.html#special-method-names" rel="noreferrer">Python</a>, <a href="https://msdn.microsoft.com/en-us/library/dd233204.aspx" rel="noreferrer">F#</a>, <a href="https://msdn.microsoft.com/en-us/library/aa288467.aspx" rel="noreferrer">C#</a>, <a href="http://dlang.org/operatoroverloading.html" rel="noreferrer">D</a>, <a href="http://www.cap-lore.com/Languages/A68Ops.html" rel="noreferrer">Algol 68</a>, <a href="http://logos.cs.uic.edu/476/resources/SmallTalk/cs476_Smalltalk/Smalltalk.htm" rel="noreferrer">Smalltalk</a>, <a href="http://www.groovy-lang.org/operators.html#Operator-Overloading" rel="noreferrer">Groovy</a>, <a href="https://design.raku.org/S06.html#Operator_overloading" rel="noreferrer">Raku (formerly Perl 6)</a>, C++, <a href="https://stackoverflow.com/a/3331974">Ruby</a>, <a href="https://stackoverflow.com/questions/16241556">Haskell</a>, <a href="https://fr.mathworks.com/help/matlab/matlab_oop/implementing-operators-for-your-class.html" rel="noreferrer">MATLAB</a>, <a href="http://se.ethz.ch/~meyer/publications/online/eiffel/basic.html" rel="noreferrer">Eiffel</a>, <a href="http://lua-users.org/wiki/MetamethodsTutorial" rel="noreferrer">Lua</a>, <a href="https://stackoverflow.com/a/1535235">Clojure</a>, <a href="http://research.physics.illinois.edu/ElectronicStructure/498-s97/comp_info/overload.html" rel="noreferrer">Fortran 90</a>, <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-ID42" rel="noreferrer">Swift</a>, <a href="http://archive.adaic.com/standards/83lrm/html/lrm-06-07.html" rel="noreferrer">Ada</a>, <a href="http://edn.embarcadero.com/article/34324" rel="noreferrer">Delphi 2005</a>...</p>\n<p>So many languages, with so many different (and sometimes opposing) philosophies, and yet they all agree on that point.</p>\n<p>Food for thought...</p>\n'}, {'answer_id': 1413443, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Saying  that operator overloading leads to logical errors of type that operator does not match the operation logic, it\'s like saying nothing. The same type of error will occur if function name is inappropriate for operation logic - so what\'s the solution: drop the ability of function usage!? \n This is a comical answer - "Inappropriate for operation logic", every parameter name, every class, function or whatever can be logicly inappropriate.\nI think that this option should be available in respectable programing language, and those that think that it\'s unsafe - hey no bothy says you have to use it. \nLets take the C#. They drooped the pointers but hey - there is \'unsafe code\' statement - program as you like on your own risk.</p>\n'}, {'answer_id': 2504988, 'author': 'Volksman', 'author_id': 257364, 'author_profile': 'https://Stackoverflow.com/users/257364', 'pm_score': 3, 'selected': False, 'text': '<p>Some people say that operator overloading in Java would lead to obsfuscation. Have those people ever stopped to look at some Java code doing some basic maths like increasing a financial value by a percentage using BigDecimal ? .... the verbosity of such an exercise becomes its own demonstration of obsfuscation. Ironically, adding operator overloading to Java would allow us to create our own Currency class which would make such mathematical code elegant and simple (less obsfuscated).</p>\n'}, {'answer_id': 8251451, 'author': 'Olai', 'author_id': 1063075, 'author_profile': 'https://Stackoverflow.com/users/1063075', 'pm_score': 3, 'selected': False, 'text': "<p>Technically, there is operator overloading in every programming language that can deal with different types of numbers, e.g. integer and real numbers. Explanation: The term overloading means that there are simply several implementations for one function. In most programming languages different implementations are provided for the operator +, one for integers, one for reals, this is called operator overloading.</p>\n\n<p>Now, many people find it strange that Java has operator overloading for the operator + for adding strings together, and from a mathematical standpoint this would be strange indeed, but seen from a programming language's developer's standpoint, there is nothing wrong with adding builtin operator overloading for the operator + for other classes e.g. String. However, most people agree that once you add builtin overloading for + for String, then it is generally a good idea to provide this functionality for the developer as well.</p>\n\n<p>A completely disagree with the fallacy that operator overloading obfuscates code, as this is left for the developer to decide. This is naïve to think, and to be quite honest, it is getting old.</p>\n\n<p>+1 for adding operator overloading in Java 8.</p>\n"}, {'answer_id': 48502000, 'author': 'Sarien', 'author_id': 1994377, 'author_profile': 'https://Stackoverflow.com/users/1994377', 'pm_score': 1, 'selected': False, 'text': '<p>This is not a good reason to disallow it but a practical one:</p>\n\n<p>People do not always use it responsibly. Look at this example from the Python library scapy:</p>\n\n<pre><code>&gt;&gt;&gt; IP()\n&lt;IP |&gt;\n&gt;&gt;&gt; IP()/TCP()\n&lt;IP frag=0 proto=TCP |&lt;TCP |&gt;&gt;\n&gt;&gt;&gt; Ether()/IP()/TCP()\n&lt;Ether type=0x800 |&lt;IP frag=0 proto=TCP |&lt;TCP |&gt;&gt;&gt;\n&gt;&gt;&gt; IP()/TCP()/"GET / HTTP/1.0\\r\\n\\r\\n"\n&lt;IP frag=0 proto=TCP |&lt;TCP |&lt;Raw load=\'GET / HTTP/1.0\\r\\n\\r\\n\' |&gt;&gt;&gt;\n&gt;&gt;&gt; Ether()/IP()/IP()/UDP()\n&lt;Ether type=0x800 |&lt;IP frag=0 proto=IP |&lt;IP frag=0 proto=UDP |&lt;UDP |&gt;&gt;&gt;&gt;\n&gt;&gt;&gt; IP(proto=55)/TCP()\n&lt;IP frag=0 proto=55 |&lt;TCP |&gt;&gt;\n</code></pre>\n\n<p>Here is the explanation:</p>\n\n<blockquote>\n  <p>The / operator has been used as a composition operator between two\n  layers. When doing so, the lower layer can have one or more of its\n  defaults fields overloaded according to the upper layer. (You still\n  can give the value you want). A string can be used as a raw layer.</p>\n</blockquote>\n'}, {'answer_id': 51715550, 'author': 'gagarwa', 'author_id': 3862024, 'author_profile': 'https://Stackoverflow.com/users/3862024', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Alternatives to Native Support of Java Operator Overloading</strong></p>\n<p>Since Java doesn\'t have operator overloading, here are some alternatives you can look into:</p>\n<ol>\n<li>Use another language.  <a href="https://en.wikipedia.org/wiki/Apache_Groovy" rel="nofollow noreferrer">Groovy</a>, <a href="https://en.wikipedia.org/wiki/Scala_(programming_language)" rel="nofollow noreferrer">Scala</a>, and Kotlin have operator overloading, and are based on Java.</li>\n<li>Use <a href="https://github.com/amelentev/java-oo" rel="nofollow noreferrer">java-oo</a>, a plugin that enables operator overloading in Java.  Note that it is NOT platform independent.  Also, it has many issues, and is not compatible with the latest releases of Java (i.e. Java 10). (<a href="https://stackoverflow.com/questions/1686699/operator-overloading-in-java/1686714">Original StackOverflow Source</a>)</li>\n<li>Use <a href="https://en.wikipedia.org/wiki/Java_Native_Interface" rel="nofollow noreferrer">JNI</a>, Java Native Interface, or alternatives.  This allows you to write C or C++ methods for use in Java.  Of course this is also NOT platform independent.</li>\n</ol>\n<p>If anyone is aware of others, please comment, and I will add it to this list.</p>\n'}, {'answer_id': 64889038, 'author': 'Audrius Meškauskas', 'author_id': 1439305, 'author_profile': 'https://Stackoverflow.com/users/1439305', 'pm_score': 1, 'selected': False, 'text': '<p>I think that people making decisions simply forgot about complex values,  matrix algebra, set theory and other cases when overloading would allow to use the standard notation without building everything into the language. Anyway, only mathematically oriented software really benefits from such features. A generic customer application almost never needs them.</p>\n<p>They arguments about the unnecessary obfuscation are obviously valid when a programmer defines some program-specific operator where it could be the function instead. A name of the function, when clearly visible, provides the hint that it does. Operator is a function without the readable name.</p>\n<p>Java is generally designed about philosophy that some extra verbosity is not bad as it makes the code more readable. Constructs that do the same just have less code to type in used to be called a &quot;syntax sugar&quot; in the past. This is very different from the Python philosophy, for instance, where shorter is near always seen as better, even if providing less context for the second reader.</p>\n'}, {'answer_id': 72372699, 'author': 'saditya', 'author_id': 14769639, 'author_profile': 'https://Stackoverflow.com/users/14769639', 'pm_score': 0, 'selected': False, 'text': "<p>Java doesn't permit operator overloading because its creator didn't add the functionality to associate different meanings with the same operator. <strong>They just wanted to keep things simple</strong> by just retaining the meaning of an operator uniform throughout the programming language.</p>\n<p>Overloading the operators makes things messy and creates a steep learning curve for new programmers. So, they just kept <strong>operator overloading</strong> out of the syllabus.</p>\n"}]
['https://Stackoverflow.com/questions/77718', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']