Why operator overloading is removed in java




















Another similar question is regarding Java being pass by reference, which mostly appears as, whether Java is pass by value or reference.

The simple and clear design was one of the goals of Java designers. They, just don't want to replicate the language, but wanted to have a clear, truly object-oriented language. Adding Operator overloading would have definitely made the design more complex than without it, and it might have lead to the more complex compiler or slows the JVM because it needs to do extra work to identify the actual meaning of operators and reduce the opportunity to optimize the language by guarantee behavior of operators in Java.

Java doesn't allow user-defined operator overloading, because if you allow a programmer to do operator overloading, they will come up with multiple meanings for the same operator, which will make the learning curve of any developer hard and things more confusing and messy.

Its been observed that there is an increase in programming errors when language supports operator overloading, which in turn increases e development and delivery time.

Since Java and JVM have taken most of the developer's responsibility, in memory management by providing garbage collector , it doesn't really make sense to left this feature to pollute the code, and as a loop hole for programming errors. From the JVM perspective, supporting operator overloading is more difficult, and if the same thing can be achieved, by using method overloading in a more intuitive and clean way, it does make sense to not support operator overloading in Java.

This is an additional benefit of not supporting operator overloading in Java. The omission of operator overloading has kept the language easier to handle and process, which in turn makes it easier to develop the tools, that process the language e. IDE or re-factoring tool. In conclusion, many things, which can be achieved by operator overloading, can also be achieved using method overloading using more intuitive and easy way and that might be the reason java designer thought that supporting operator overloading will not be a big benefit for language, but in fact, only Java designer can answer real motivation of, why Java doesn't support operator overloading, like some other questions as Why Java doesn't support multiple inheritances or Why String is immutable in Java.

Share to Twitter Share to Facebook. Labels: core java , core java interview question. September 9, at AM Anonymous said September 9, at PM Anonymous said September 10, at AM Anonymous said October 4, at AM killy said January 18, at AM Anonymous said February 13, at AM Partha said February 17, at AM Anonymous said Can any body let me know the final answer of the question" Why java does not support operator overloading" November 27, at PM Unknown said January 12, at PM javin paul said January 13, at AM Anonymous said Can anybody answer the actual question here?

December 13, at PM Anonymous said Newer Post Older Post Home. Subscribe to: Post Comments Atom. Subscribe for Discounts and Updates Follow. Search This Blog. Interview Questions core java interview question data structure and algorithm 78 Coding Interview Question 75 interview questions 70 design patterns 35 SQL Interview Questions 34 object oriented programming 34 thread interview questions 30 spring interview questions 28 collections interview questions 25 database interview questions 16 servlet interview questions 15 Programming interview question 6 hibernate interview questions 6.

How to design a vending machine in Java? How HashMap works in Java? Why String is Immutable in Java?

Translate This Blog. How to Solve java. ClassNotFoundException: org How to use var to declare local variables in Java? Example What is SuppressWarnings annotation in Java? Difference between Process and Thread in Java - Ex Spring Transactional Annotation Example - How to How to create thread safe Singleton in Java - Java How to Code in Dart Programing language? Moreover, while working with programming languages that support operator overloading the program error rate is high compared to others.

Your comment on this answer: Your name to display optional : Email me at this address if a comment is added after mine: Email me if a comment is added after mine Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java. Does Java support Default Parameters? Does the finally block always execute in Java? What does Java option -Xmx stand for? How to execute a python file with few arguments in java? What does NaN mean in Java? Can quotes be added in a java string. Welcome back to the World's most active Tech Community! Operator overloading might not seem to be a good idea outside arithmetics as a lot of people pointed out here.

But if you have to write software in the domain of arithmetics, for example 3D applications, cryptographic software, financial software or any kind of scientific software you are really missing operator overloading in Java a lot.

Just think about something like rendering algorithms like raytracing, pathtracing, ambient occlusion and such stuff where you have to compute the orthonormal base.

Sqrt 1. There are much more complicated formulas and things can really get nearly unreadable when it gets more complicated. It can also be a source of errors when translating formulas from paper in your programming language since it is not easy to see on first sight if the translated formula is the same as on paper.

Another bad things when not having operator overloading in the language is that you have to memorize the function names for doing arithmetics. And for me it is really a reason not to use it for that kind of software. I think the problem is not operator overloading per se, but abusing it can cause problem. There is no different from normal Java method. Like if we have method add, but when call it, it will read data from database and display it not add anything , is Java method a problem?

Take examples from the natural origin of computer science: mathematics. The operator has meaning for numbers, matrices, vectors with a number etc. In this case it is overloaded. What does it mean? This means that operator overloading makes sense in some cases. In Mathematics it is also strictly defined how the operators work, so that everyone knows what they mean and everyone is happy with them.

In the world of programming languages overloading the operators makes sense when you design a DSL Domain specific language. In the context of this language the operators will do what they are intended to do and what they are documented to do hopefully. Aaron Novstrup Aaron Novstrup Add a comment. I believe ML handles this entirely in parsing, not lexing, but if you took this basic concept enough further, I can believe it might start to affect lexing, not just parsing.

Not to mention that most Java tools will use the JDK, which has a complete Java compiler built into the JVM, so tools can normally do most such analysis without dealing directly with parsing and such at all. Jerry Coffin Jerry Coffin k 74 74 gold badges silver badges bronze badges. Try doing extensive work with BigInteger and you may start to come around to the idea that maybe all operator overloading isn't bad.

It can't be that hard, can it?! Taycher: the parser as such isn't normally "aware" of spaces or lack thereof at all. Those are handled by the lexer, with no "awareness" of operator overloading at all. Each has good and bad points, but they both work. Colin Hebert Colin Hebert So much for simplicity.

In fairness it's different people at completely different times. Not that the generics actually were worth anything in Java. This declaration can be found in the Java Language Environment whitepaper : There are no means provided by which programmers can overload the standard arithmetic operators. In my personal opinion, that is a wise decision. Vineet Reynolds Vineet Reynolds I prefer languages that do not allow me to do this - define private public or their variants thereof.

I've always hated the logic that "because a bad programmer can use this functionality to write bad code, this functionality is bad". There are plenty of cases where operators in the code would be far clearer than method names. Knowing when to use operator overload and when not to is just one sign of a better programmer. You could just as easily say "being able to have two methods with the same name is bad, because someone might name all their methods the same name even if they do something different".

Whether we agree with the Java designers or not, this argument is one of the reasons that Java does not support operator overloading. Vineet, Bad programmers will write bad code in any language. Sure, some language features make it easier or harder, but those same language features can have the exact opposite effect for good programmers making code clearer used by a good programmer vs uglier used by a bad programmer. Just because people are injured in car accidents doesn't mean we should all be riding horses.

Today, in software industry, there are, mainly, two different types of languages: Low level languages High level languages This distinction was useful about 10 years before now, the situation, at present, is a bit different. I know, maybe I was a little long, but hope it helps. I might not agree with everything in this post, but at least you really thought it through, unlike some others who posted an answer.

Incognito Incognito GreenMatt GreenMatt I think this is the best answer here. Not opinion based at all. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked 1. Related Hot Network Questions.



0コメント

  • 1000 / 1000