Why Doesn't Java Offer Operator Overloading?
Coming from C++ to Java, the Obvious Unanswered Question Is Why Didn't Java Include Operator Overloading? Isn't Complex A, B, C; a = B + C; Much Simpler Than...
Coming from C++ to Java, the obvious unanswered question is why didn't Java include operator overloading?
Isn't Complex a, b, c; a = b + c; much simpler than Complex a, b, c; a = b.add(c);?
Is there a known reason for this, valid arguments for not allowing operator overloading? Is the reason arbitrary, or lost to time?
17 Answers
There are a lot of posts complaining about operator overloading.
I felt I had to clarify the "operator overloading" concepts, offering an alternative viewpoint on this concept.
Code obfuscating?
This argument is a fallacy.
Must Read
Obfuscating is possible in all languages...
It is as easy to obfuscate code in C or Java through functions/methods as it is in C++ through operator overloads:
// C++
T operator + (const T & a, const T & b) // add ?
{
T c ;
c.value = a.value - b.value ; // subtract !!!
return c ;
}
// Java
static T add (T a, T b) // add ?
{
T c = new T() ;
c.value = a.value - b.value ; // subtract !!!
return c ;
}
/* C */
T add (T a, T b) /* add ? */
{
T c ;
c.value = a.value - b.value ; /* subtract !!! */
return c ;
}