java - 从什么时候开始使用运算符 + 进行字符串连接?

标签 java operator-overloading string-concatenation

所以我的问题相当简单;

  1. 谁是第一个使用加法运算符进行字符串连接的人?
  2. 如果是 Java,他们为何决定这样做?
    • Sun 似乎明确禁止 Java 中的运算符重载,因为它允许您重新定义运算符,这会打破人们对该运算符的假设。
    • 然而他们决定实现一个字符串连接加法运算符,这违反了加法运算符的交换律?

最佳答案

根据java,编写代码的程序员不能重载运算符,但就java语言而言,+运算符被重载,它对基元(例如int和double)和String对象执行加法。

In a language that supports operator overloading like C++, you can turn a + operator to perform a subtraction, resulted in poor codes.That is the case Java designers doesn't allow programmer to overloade a operator.

在 Java 中,String 对象在 Java 中受到特殊处理,因为它们在程序中经常使用。基元存储在调用堆栈中,这需要更少的存储空间并且操作起来更便宜。另一方面,对象存储在程序堆中,需要复杂的内存管理和更多的存储空间。
出于性能原因,Java 的 String 被设计为介于基元和类之间。

String literals are stored in a common pool. This facilitates sharing of storage for strings with the same contents to conserve storage.This is also a case The Java designers think to overload + for String concatination to improve performance and give convenience to programmer.

关于java - 从什么时候开始使用运算符 + 进行字符串连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18695207/

相关文章:

javascript - 在移动设备上确定位置时,什么比 GPS 更准确

Java泛型抽象类返回类型不匹配

C++ Eigen : subclassed vector get error in conversion with simple operation

c# - 进行字符串连接时的性能 - 算法字符串字符串 c#

c++ - 如何连接 char* 和 LPWSTR 字符串?

java - 参数和 Android list

java - 如何使用 USAEPAY Web 服务删除信用卡?

oop - matlab subsref : {} with string argument fails, 为什么?

c++ - 在 C++ 中重载 [] 以返回左值

java字符串连接奇怪的行为