File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why didn't java support operator overloading

 
satish chander
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to overload operators in java
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Java doesn't support operator overloading. I think one main reason is that it's too easy to misuse operator overloading or to get it simply wrong.

Marco
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct; people abused operator overloading in C++, so it was completely removed as a possibility. IMO this is too bad, but I understand the logic behind the decision.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I for one am glad that Java doesn't support it. I've yet to see a good example for where it makes sense (beyond trivial ones like implementing data types like complex numbers).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String concatenation and collection appending are the two I really appreciate. It can also be *really* handy in DSLs.
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I for one am glad that Java doesn't support it. I've yet to see a good example for where it makes sense (beyond trivial ones like implementing data types like complex numbers).


agree++
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:collection appending

I don't see how "c1 = c1 + c2" is that much easier than "c1.addAll(c2)". Another issue is what the type of "c1 + c2" should be. The type of c1? The type of c2? Something else? Or would you only want to allow operators when combined with assignment, like "c1 += c2"? In that case the bulk operators are more than adequate.

The only classes for which I think mathematical operators should be overloaded are BigInteger and BigDecimal. Both are immutable classes that represent mathematical types. I really hope that the rumours of Sun adding them in Java 7 are true. Unlike my example of collection appending, "b1 * b2" is a lot better than "b2.multiply(b2)" because it's more natural.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people don't, but for *me*, it *is* easier, and has less cognitive overhead. Or c << c1, which has even less.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:agree++

Is agree an int? Or are you overloading the ++ operator here?

In my opinion it would have been good if Java had operator overloading. That would have made the wrapper classes (Integer, Long, etc.) and classes like BigInteger and BigDecimal easier to use.
 
Ben Logan
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those great evil-or-brilliant arguments. Like multiple inheritance!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote C++ for years, and I don't miss it one iota.

I don't think that the << construct for string concatenation (often put forward as the case for operator overloading, just as in this thread) is any clearer than chained appends on a string builder.

To each, I say.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was collection appending. I'd rather see my code than read it, and not have the *language* determine how I should program, to the extent possible.
 
Henry Wong
author
Marshal
Pie
Posts: 20820
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I wrote C++ for years, and I don't miss it one iota.


I still code in C++ ... well, 20% of my time anyway.

Back in the days, when I just started coding in C++ (and at 100% of the time), my pet peeve was the cout syntax. How is shifting left to mean writing to a file intuitive? It was just silly to me. Add to that, all the operator overloading that was done because someone thought it was cool. Sheesh.

Today, luckily, I think most programmers learned the lesson and only use operator overloading sparingly. Even so, when I encounter a class, that I didn't write, for the first time, I always go through the definition in the include file to check for any operator overloading.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:In my opinion it would have been good if Java had operator overloading. That would have made the wrapper classes (Integer, Long, etc.) and classes like BigInteger and BigDecimal easier to use.

Fortunately, with auto(un)boxing, you get to use mathematical operators on all wrapper classes. That only leaves BigInteger and BigDecimal, and as I've said, there are rumours that they will get special treatment by the compiler just like String has.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not impressed by special-case compiler hacks when it should be handled cleanly by the language. And I shouldn't have to hack a compiler to get the same functionality for Complex, Matrix, or whatever else I want to do, either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic