Sunday, November 18, 2012

Operator overloading in Java

Java doesn't allow the programmer to overload operators e.g. +,-,/,*. Java left some of the features that are supported in C++ e.g. no pointers in Java , no pass by reference in Java and no support for multiple inheritance .




Most people will think that + is overloaded in Java . For example
int a=4+3;
String blog="java"+"latte";
+is used for for addition and string concatenation.
Here is the misunderstanding what operator overloading actually means.

While technically speaking, Java does not support operator overloading at all. The behavior of the two '+' operator with respect to String and numeric type do give the appearance of single operator overload with respect to programmer aspect. 
This is fundamentally different from operator overloading defined by C++ and C# for example.

Let me give you an example

The Java language provide the special support for string concatenation operator (+). String concatenation is implemented through the StringBuffer ( or String Builder ) class and its append methods. String conversions are implemented the string method toString(), defined by Object class and inherited by the all java classes. 

Unlike c++, this is compile time operation ... not operator overloading.when you type.....


String blog="Java-"+"latte"+".blogspot."+"in"+5;


compiler change this to


String blog=new StringBuilder("Java-").append("latte").append(".blogspot.").append("in").append(new Integer(5).toString()).toString().


This is not operator overloading. It is also not a runtime expression.It is not a part of string class. 
You can think of it as macro being expanded at compile time if you compare it to something from C++ perspective.

Most basic reason behind not supporting operator overloading is 


1. Simplicity and cleanliness 

C++ has proven by example that operator overloading makes code almost impossible to maintain.Simple and clear design is goal of the Java designer,they don't want to replicate the language but want to have clear, truly OO language. Operator overloading make the design complex and also slow the JVM because it need to do extra work to identify the actual meaning of operator and reduce the opportunity to optimize language by guarantee behavior of operator in Java.

2. Avoid programming Error

If you allow programmer to do operator overloading they will come with different meaning of the same operator, it will make difficult to learn and make things more confusing and messing.

3. JVM complexity

From JVM perspective operator overloading is more difficult. If same thing can be achieved by using method overloading in more intuitive and clean way, it does not make sense to support operator overloading.

Conclusion:

Things which can be achieved through operator overloading can be achieved through method overloading using more intuitive and clean way, that might be the reason behind not supporting operator overloading.

When Java Developer explains to developer why operator overloading is useless.





2 comments:

  1. A lot of this is plain incorrect.

    For example, the statement "From JVM perspective operator overloading is more difficult", is wrong. Why and how would it matter to the JVM which compiles bytecode? Operators are literally just functions. The compiler could translate them appropriately as the compilers for dozens of other languages do.

    Also, you state that "If same thing can be achieved by using method overloading in more intuitive and clean way, it does not make sense to support operator overloading.", but how is myBigInt.Add(myOtherBigInt) more intuitive than myBigInt + myOtherBigInt? It is not.

    Furthermore,
    String blog=new StringBuilder("Java-").append("latte").append(".blogspot.").append("in").append(new Integer(5).toString()).toString();

    Is most definitely a runtime operation. The reason operator + works on strings is because strings are treated specially by the language.

    ReplyDelete
  2. I experience very grateful that I read this. It's miles very helpful and very informative and that i certainly discovered plenty from it.
    logo design coimbatore
    brochure design coimbatore
    seo company in coimbatore
    digital marketing in coimbatore

    ReplyDelete