• Post Reply Bookmark Topic Watch Topic
  • New Topic

Re: Operator Overloading  RSS feed

 
NitishK Kumar
Ranch Hand
Posts: 40
Chrome Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Java does n't support operator overloading?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem with some "why" questions is that you need the original java designers to answer them -- all we can do is speculate.

However, my guess would be the designers felt that it made the C++ language too complicated for the value that it was bringing, and decided against it.

Henry
 
NitishK Kumar
Ranch Hand
Posts: 40
Chrome Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my question is wrong. I mean to ask why it does not support user defined operator overloading.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly the question that Henry addressed.

Let's turn this around -- why don't you explain why it should?
 
NitishK Kumar
Ranch Hand
Posts: 40
Chrome Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:That is exactly the question that Henry addressed.

Let's turn this around -- why don't you explain why it should?


Sir, I got my answer, it was done to keep the language clean as assigning multiple meanings to single operator will lead to more programming errors. Why I got this doubt because people say that it doesn't support operator overloading, while actually it does. For instance, + is used in string concatenation as well as arithmetic operations and this in-built feature of java.
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NitishK Kumar wrote:while actually it does. For instance, + is used in string concatenation as well as arithmetic operations and this in-built feature of java.


I think that's the only place where it does.

Just think of overloading the operator say "-" to do addition? So one can really not think of an use case here with operator overloading. We would be more than happy to have what the default behavior is for the operators.
 
Campbell Ritchie
Marshal
Posts: 55788
164
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The - operator is overloaded; it can be a binary operator meaning subtraction or a unary operator meaning sign change.
The + operator is overloaded; it means addition if used as a binary operator with numbers, catenation as a binary operator with Strings, and promotion as a unary operator with numbers.
The &, | and ^ operators are overloaded, for bitwise "AND" "OR" and "XOR" with (integer) numbers and logical "AND", "OR", and "XOR" with booleans.
Some people also say the ++ and -- operators are overloaded because they can be used as unary prefix and unary postfix operators. Some people also say the + - / * and % operators ought to be overloaded to take BigDecimal and BigInteger as operands.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!