File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why didn't java support operator overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why didn Watch "Why didn New topic
Author

Why didn't java support operator overloading

satish chander
Greenhorn

Joined: Dec 21, 2009
Posts: 12
is it possible to overload operators in java
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Sep 29, 2008
Posts: 12617

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
Marshal

Joined: Mar 22, 2005
Posts: 41173
    
  45
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).


Ping & DNS - my free Android networking tools app
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

String concatenation and collection appending are the two I really appreciate. It can also be *really* handy in DSLs.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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++


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ben Logan
Greenhorn

Joined: Feb 13, 2010
Posts: 12
This is one of those great evil-or-brilliant arguments. Like multiple inheritance!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why didn't java support operator overloading
 
Similar Threads
zipping as self extracting exe
J2EE
class
WA #2 ..... word association
c++ or Java--your suggestions please