Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java need Operator Overloading? [closed]

Why doesn't Java need operator overloading? Is there any way it can be supported in Java?

like image 272
pure.java Avatar asked Aug 24 '10 18:08

pure.java


2 Answers

Java only allows arithmetic operations on elementary numeric types. It's a mixed blessing, because although it's convenient to define operators on other types (like complex numbers, vectors etc), there are always implementation-dependent idiosyncrasies. So operators don't always do what you expect them to do. By avoiding operator overloading, it's more transparent which function is called when. A wise design move in some people's eyes.

like image 141
Sanjay Manohar Avatar answered Oct 19 '22 14:10

Sanjay Manohar


Java doesn't "need" operator overloading, because no language needs it.

a + b is just "syntactic sugar" for a.Add(b) (actually, some would argue that a.Add(b) is just syntactic sugar for Add(a,b))

like image 43
James Curran Avatar answered Oct 19 '22 16:10

James Curran