java operator overload_Java cannot overload any operators. Why? [duplicate]

问题

This question already has answers here:

Closed 9 years ago.Possible Duplicate:

Java operator overload

In c++, we can perform the operator overloading. But Java is also a Object oriented language. So why java doesn't support overloading?

回答1:

Actually, it does support operator overloading... of a very limited, built-in only nature. For instance "+" is overloaded for String's in addition to the usual arithmetic.

Of course, most people want to know why Java does not support user-defined operator overloading. :-) The simplest answer seems to be that the Java creators did not, at the time, see any clean way to add it to the language without making Java a mess (like C++) in the process.

回答2:

http://java.sun.com/docs/white/langenv/Simple.doc2.html

There are no means provided by which

programmers can overload the standard

arithmetic operators. Once again, the

effects of operator overloading can be

just as easily achieved by declaring a

class, appropriate instance variables,

and appropriate methods to manipulate

those variables. Eliminating operator

overloading leads to great

simplification of code.

The last statement is of course very subjective.

回答3:

it's design goals to make java simpler than c++

We wanted to build a system that could

be programmed easily without a lot of

esoteric training and which leveraged

today’s standard practice. So even

though we found that C++ was

unsuitable, we designed Java as

closely to C++ as possible in order to

make the system more comprehensible.

Java omits many rarely used, poorly

understood, confusing features of C++

that, in our experience, bring more

grief than benefit.

from here:

The Java Language: An Overview

回答4:

The code is harder to understand when you use operator overloading in c++. May be that was the reason why Java developers decided not to implement it.

Really the code rich with overloaded operators can be very misleading and hard to read, just like the code with a plenty of macros.

回答5:

Note that there's an anomaly in that the 'plus' operator is overloaded for java.lang.String.

回答6:

Way back when, the news was that the Java team looked at other languages (primarily C++) and tried to make a judgment call as to what language level features to include (or exclude) from Java.

Operator overloading was a big new feature in C++ and lots of budding programmers used it to solve problems in interesting ways. Unfortunately, most of those ways were buggy, and few programmers overloaded operators in ways that were "programmer portable". As a result, reading a program would often miss that "magic" code had been introduced in the "+" operator (or something else). Outside of numbers (vectors, etc.) you also had disagreements as to what "+" might mean.

So the cover story (no idea if it was true) was that the Java team saw that you could just name methods sum.plus(...) and it would be obvious that the call was to user written code (which could be overloaded) while the reserved operations "+", etc. would only did what the language defined. This would increase some types of code readability at the expense of a few extra words.

Is it true, or is it an attempt to back fill history with rationalizations? I don't know. Perhaps the language implementers just were busy trying to get Java working and didn't get around to operator overloading by release date. Either way, it greatly increases the readability of Java in 95% of all cases, at the expense of making vector math a bit wordy.

来源:https://stackoverflow.com/questions/2629501/java-cannot-overload-any-operators-why

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值