• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Java does not support Operator overloading?  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Java does not support operator overloading?
well, I think it would need more complex compiler and JVM. Or java do not want user to override operator's default working.
But i know its importance. Default operator definition is for primitive types only but what for new types like classes. anyway there must be a genuine reason for it too. I Want to know.
Please sir/mam, Do reply to this and I want exact reason.
Thank you.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaspreet,
Welcome to CodeRanch!

My gut reaction is "Because the language designers decided not to". I don't know why they chose not to.

I don't miss it. new BigDecimal(1).add(new BigDecimal(3)) is readable. There's an article on DZone suggesting it isn't needed.
 
Jaspreet Saran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir/ mamomeone ask me this q but what should i say if suestion on an interview?
As usually they ask why there are no pointers in java....and many suck things.
well there are pointers in java too but only JVM can understand them and can use them. we cannot. this add to the security feature of java.
... while i was writing code to add two complex numbers ( I am a beginner to java So don't know the API's class to do this, so i made my own, its simplest thing)...
then this question arose in my mind. well if java support it would be helpful.
anyway if it doesn't no problem.
again i need some clearer reason if anybody can, please?
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that this question is meant to get you talking, not to elicit any explicit answer (which would be "Because the language designers decided not to" like Jeanne said). It tests what you know about the Java language and its design, as well as the pros and cons of operator overloading in general.

As an irrelevant aside, I for one am glad that it's not supported. Looking at what operator overloading got used for in C++, it can get really messy, especially where "+" does not have a "natural" meaning, like it does for addition (or, by custom, for string concatenation).
 
Ranch Hand
Posts: 3090
14
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:My gut reaction is "Because the language designers decided not to". I don't know why they chose not to.

I would expand on this by saying that Java's designers were reacting to the perceived excesses of C++. They felt that people had overused operator overloading to create overly complex code, hard to read and hard to maintain. So they chose to ban it, after convincing themselves it wasn't really necessary.

I would give almost exactly the same answer to "why doesn't Java support multiple inheritance?"

Personally I would prefer to have both those things, within reason. In the case of overloading, I really do find math formulas much nicer to deal with when they look like math formulas. Sure, we may say that a.plus(b) is "readable", since it's so short. But try representing the quadratic equation that way, and spot an error in implementation. I would argue that from a serious math/science/engineering perspective, math is not pleasant to look at in Java. I miss Fortran.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have to admit, I really love operator overloading. I can make a class do anything that I want using expressions alone. Operator overloading is one of the features that I really love about C++ !!

On the other hand, I really hated it when other people used operator overloading. Really? That expression makes sense? And seriously?? You can overload the new and delete operator? Who does that? Okay, so operator overloading is also one of the features that I really hate about C++ !!

Hmmm... now I can't decide ...

Henry
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaspreet Saran wrote:thank you sir/ mamomeone ask me this q but what should i say if suestion on an interview?

My answer would be "I don't know". And what's more: I don't really care (although I probably wouldn't say that ).

Why worry about something that a language doesn't support? I suppose there might be some intellectual curiosity involved, but otherwise it seems like a waste of energy.

And oddly enough it's one of the questions in our DontSweatIt page - specifically this section.

Personally, I hated operator overloading in C++, and almost never used it myself. It just seemed to produce more problems (and code) than it was worth. I can see a case for allowing '+' with Strings (which Java does), and maybe for strictly numeric classes (which it doesn't), but where does it end? If you're going to be consistent about it, then there are at least 11 operators I can think of that virtually every numeric value would need to support (+, -, *, /, %, prefix+, prefix-. and prefix AND postfix increment/decrement). And that's before you get into all the business of precedence.

Too much for me. I'm much happier with add(), subtract(), multiply() and divide().

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why worry about something that a language doesn't support? I suppose there might be some intellectual curiosity involved, but otherwise it seems like a waste of energy.

As I mentioned in my previous answer, I think it's one of those interview questions that is not designed to elicit any particular answer, but rather to start an open-ended discussion to get the candidate to talk freely -in this case about OOP features and languages-, and to get a sense of how he handles that.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was also probably a better question back in the early days of Java - because back then, C/C++ was the standard point of comparison, and the differences were the primary way they explained the language to people new to it. The answer was fairly well-known at the time. Nowadays it's more like irrelevant trivia.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I think it's one of those interview questions that is not designed to elicit any particular answer, but rather to start an open-ended discussion to get the candidate to talk freely -in this case about OOP features and languages-, and to get a sense of how he handles that.

Yeah, I can see that; but it seems to me that it might be more productive to focus on something that Java does support, but does a particular way - like single inheritance - rather than something that a Java programmer might never have seen before.

It's actually kind of an interesting question: is operator overloading (or indeed any overloading) OOP?

Winston
 
Jaspreet Saran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for replying to this question....n sorry my keyboard was not working so there are lot of spelling mistakes in my reply.....Well i got it shy java doesn't support....but i would love it if it support.....because i can make a class with which i can play with operators too.....but i understood that it also lessen the security of java....as so many can misuse the operators over loading...so they did not add this feature...ok thanks again.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say the reasons had to do with security (and nobody else said that either, AFAICT). The "misuse" is more at the level that it makes the code harder to understand - aside from mathematical operations, it's not intuitively clear what a "+" operator might do to two objects.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think of below reasons :

1) Simplicity and Cleanliness
2) Avoid Programming Errors
3) JVM Complexity
4) Easy Development of Tools
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:It's actually kind of an interesting question: is operator overloading (or indeed any overloading) OOP?


Well, you can take the view of a number of languages like Scala: operators are not really operators. They are methods, that just happen to be able to be used using infix notation as syntactic sugar because people are used to seeing operators used like that. As such, they are exactly as OOP as other methods .
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. Scala's approach seems more OOP to me, as it puts all types on an equal footing as objects, with methods that can be overridden (when not final, etc). Getting rid of special cases for primitives and operators.

Which leaves us with the second half of Winston's comment: is there something non-OOP about any overloading? I don't think so - to me, it's just a way to create additional methods, without needing to create new names. We could get the same effect without overloads; we'd just have many more method names, and they'd probably be longer. Java's method names are long enough already, I think.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!