为什么 +在 Java 中使用字符串?
Java 无法进行运算符重载,但 +
对于 String
和 Integer
以及其他一些类可以正常工作。这怎么可能?
更新:
为什么这有效?
Integer i = 4;
Integer p = 5;
System.out.println(i*p); // prints 20
Java can't do operator overloading, but +
works okay for String
and Integer
and some other classes. How is this possible?
update:
Why does this work?
Integer i = 4;
Integer p = 5;
System.out.println(i*p); // prints 20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
+
不是运算符重载的示例。+
内置于语言中,作为连接运算符和算术加法运算符。这意味着用Java编写程序的人不能重载运算符,但就Java语言的语法而言,
+
被定义为连接和加法运算符。编辑
它适用于其他类,例如
Integer
和Double
,因为自动装箱。如果您查看执行字符串连接的 Java 程序的字节码,您将看到它创建
StringBuilder
并使用append()
方法。 Java 编译器看到+
运算符并意识到操作数是字符串而不是基本类型(如int
)。如果您查看执行整数加法的程序的字节码,您会发现它使用 iadd 指令来执行整数加法。这是因为编译器意识到
+
运算的操作数是整数。至于执行诸如
Integer i = 4
之类的操作,字节码将显示您实际上正在执行Integer i = Integer.valueOf(4)
。这称为自动装箱。稍后,当您执行类似i + p
的操作时,其中i
和p
的类型都是Integer
,生成的字节码将显示您正在执行i.intValue() + p.intValue()
,其中两个方法的返回类型都是int
(实际的字节码指令再次强调,是iadd
)。这就是为什么
+
可以使用Integer
,即使它们不是实际的基本类型。+
is not an example of operator overloading.+
is built into the language as a concatentation operator and an arithmetic-addition operator.What this means is that a person writing a program with Java cannot overload operators, but as far as the grammar of the Java language is concerned,
+
is defined as a concatenation and an addition operator.EDIT
It works for other classes such as
Integer
andDouble
because of autoboxing.If you take a look at the bytecode of a Java program that performs string concatenation, you'll see that it creates
StringBuilder
and uses theappend()
method. The Java compiler sees the+
operator and realizes that the operands are strings and not primitive types (likeint
).If you look at the bytecode of a program that does integer addition, you will see that it uses the
iadd
instruction to perform integer addition. This is because the compiler realizes that the operands to the+
operation are integers.As far as doing something like
Integer i = 4
, the bytecode will show that you're actually doingInteger i = Integer.valueOf(4)
. This is called autoboxing. Later on, when you do something likei + p
, where bothi
andp
are of typeInteger
, the generated bytecode will show that you're doingi.intValue() + p.intValue()
, where the return types of both methods areint
(the actual bytecode instruction again, isiadd
).This is why
+
worksInteger
even though they are not actual primitive types.由于自动装箱,它适用于像 Integer 这样的原始包装器。
它适用于字符串,因为这是连接字符串的特殊情况:
It works for primitive wrappers like Integer because of autoboxing.
It works for String because that's a special case for concatenating strings:
+
是一个内置操作。这是一个例外,而不是规则。+
is a built-in operation. It's an exception, not a rule.Java 不允许自定义运算符重载,但编译器开发人员仍然可以告诉编译器 String1 + String2 == String1String2,并用正确的串联方法调用替换 + 运算符。
Java doesn't allow custom operator overloading, but the compiler can still be told by the compiler developer that String1 + String2 == String1String2, and to substitute the proper concatenation method call for the + operator.
实际上执行的是
What actually is execute is
正如@yan 所说,这是例外,而不是规则。字符串在 Java 中具有特殊的地位。 Java 语言规范中有一个完整的小节专门介绍了
+
作为字符串连接运算符的作用:§15.18.1。关于您的更新,这是另一个特殊情况。 Java有时,根据具体情况,足够聪明,可以在
String
时将非String
的内容转换为String
需要代码>。这些特殊情况之一就是您所描述的情况,其中基元出现在需要String
的位置。基元首先被转换为其引用类型 -Integer
、Double
和 &c。 — 然后通过toString()
方法转换为String
。另一种特殊情况是当一个
String
和一个非String
使用字符串连接运算符+
组合时,如 JLS §5.4 — 字符串转换。为了完整起见:
+
更常见的“将数字相加”的作用在 §15.18 的其他部分中进行了描述,§15.18.2 — 数字类型的加法运算符(+ 和 -)。As @yan said, this is the exception, not the rule. Strings have a special status in Java. There's a whole subsection of the Java Language Specification devoted to
+
in its role as the string concatenation operator: §15.18.1.Regarding your update, that's another special case. Java is sometimes, depending on the case, smart enough to convert things that are not
String
s intoString
s whenString
s are needed. One of these special cases is the one you described, where primitives are showing up in a place that needs aString
. The primitives are first converted to their reference types —Integer
,Double
, &c. — and then intoString
s via thetoString()
method.Another special case is when one
String
and one non-String
are being combined with the string concatenation operator+
, as described in JLS §5.4 — String Conversion.For completeness:
+
in its more common "adding numbers together" role is described in the other part of of §15.18, §15.18.2 — Additive Operators (+ and -) for Numeric Types.