Method overloading is one of the main tenets to object orientated programming and Java as a whole, but when it comes to operator overloading things are not a simple in Java.
Java’s design leans heavily on the (correct) belief that the average programmer is imperfect, lazy, and ill-informed. In response, Java acts like a typical overbearing parent. Java knows that you’re too lazy to check whether or not you’re REALLY writing inside of array bounds, so Java does it for you. Programmers are too forgetful to free all memory that they have initialized. Java does it for you. In fact, pointers are hard, so you don’t get to use them in the traditional sense. At all. Maybe when you’re older. However, operator overloading must have really burned the designers at Sun, because overriding operators isn’t in the language. Maybe the designers were offended that C++ overrode the bit-shift operators (<< and >>) for input and output. Maybe they came across a home-grown database system that used every operator under the Sun to perform tasks. Whatever the reason, it’s just not in the language. Well, almost not in the language. As a convenience, they decided to throw C++’s unwashed masses a bone when it came to Strings. You get the ‘+‘ operator. String a = "Hello " + "World!"; Do you feel the wind rushing through your hair yet? Me neither. According to the documentation this is just syntactic sugar, as Java treats this as using StringBuilder or a StringBuffer along with append methods. Consequences In the event that you don’t think that the lack of operator overloading is harmful, let’s consider the discriminant in the quadratic formula. // a, b, c are floats discriminant = b*b - 4*a*c; This looks pretty easy, right? Now let’s look at it as a BigDecimal: // a, b, c are BigDecimals discriminant = (b.multiply(b)).subtract((BigDecimal(4)).mult(a).mult(c)); This doesn’t look awful, but saying that it’s as readable as the normal arithmetic example is wrong. You could try to make it as neat and pretty as you can by commenting and indenting and spreading it out. I would, within reason. Do you think that this still doesn’t look bad? Then try doing the cubic formula. Or the quartic. The syntax is going to get in your way. For big or complex implementations, this can be a pain in the ass. It becomes its own issue for the programmer, and gets directly in the way of actually solving problems. The Solution to Java’s Woes I always feel bad complaining about something without trying to come up with a solution (one of the skills from my reading comprehension toolkit) so I tried to develop a Java-y solution that lets you overload operators while still “protecting the programmer from himself”. My solution is based on a single premise and two added constructs. If we let the programmer override the base functions multiply(), divide(), subtract(), and add(), they will be allowed to use the overloaded operators (it’s not my fault/problem the designers of ArrayList used add instead of append() or insert()). For our discriminant example, this means that we are able to write b^2 as we would expect to if BigDecimal had multiply overridden. It will just automatically be called. Now, here is the premise that will hold it all together. * You can only use type a’s operators with other objects of type a. In order to make this useful, a second construct will be added to classes, the convert() construct. Let’s say the user wants to add two different types, such as the following example: BigDecimal a; BigInteger b; a = a + b; The user will have to override the convert() method in BigDecimal. The class would end up looking like this: class BigDecimal{ public static BigDecimal add(BigDecimal a){ // ... } public static BigDecimal convert(BigInteger a){ // ... } } The example would then be expanded to the following: BigDecimal a; BigInteger b; a = a.add(BigDecimal.convert(b)); There we go, it’s easy! The Java compiler/interpreter can automatically perform this work just as it does for the String. It’s really no different, the mechanism is already built into the language. All that’s missing is order of operations, and that could easily be taken care of by processing the lines with a few passes. If you don’t like this, what’s your way?
Read Jake Voytko complete post: http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/