• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why didn't java support operator overloading

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to overload operators in java
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String concatenation and collection appending are the two I really appreciate. It can also be *really* handy in DSLs.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of those great evil-or-brilliant arguments. Like multiple inheritance!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic