为什么 +在 Java 中使用字符串?

发布于 2024-10-28 09:37:50 字数 242 浏览 1 评论 0原文

Java 无法进行运算符重载,但 + 对于 StringInteger 以及其他一些类可以正常工作。这怎么可能?

更新:
为什么这有效?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉殇 2024-11-04 09:37:50

+ 不是运算符重载的示例。 + 内置于语言中,作为连接运算符算术加法运算符。

这意味着用Java编写程序的人不能重载运算符,但就Java语言的语法而言,+被定义为连接和加法运算符。

编辑

它适用于其他类,例如IntegerDouble,因为自动装箱

如果您查看执行字符串连接的 Java 程序的字节码,您将看到它创建 StringBuilder 并使用 append() 方法。 Java 编译器看到 + 运算符并意识到操作数是字符串而不是基本类型(如 int)。

如果您查看执行整数加法的程序的字节码,您会发现它使用 iadd 指令来执行整数加法。这是因为编译器意识到 + 运算的操作数是整数。

至于执行诸如 Integer i = 4 之类的操作,字节码将显示您实际上正在执行 Integer i = Integer.valueOf(4)。这称为自动装箱。稍后,当您执行类似 i + p 的操作时,其中 ip 的类型都是 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 and Double 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 the append() method. The Java compiler sees the + operator and realizes that the operands are strings and not primitive types (like int).

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 doing Integer i = Integer.valueOf(4). This is called autoboxing. Later on, when you do something like i + p, where both i and p are of type Integer, the generated bytecode will show that you're doing i.intValue() + p.intValue(), where the return types of both methods are int (the actual bytecode instruction again, is iadd).

This is why + works Integer even though they are not actual primitive types.

☆獨立☆ 2024-11-04 09:37:50

由于自动装箱,它适用于像 Integer 这样的原始包装器。

它适用于字符串,因为这是连接字符串的特殊情况

Java 语言为字符串连接运算符 (+) 以及将其他对象转换为字符串提供了特殊支持。字符串连接是通过StringBuilder(或StringBuffer)类及其append方法实现的。字符串转换是通过toString方法实现的,该方法由Object定义,并被Java中的所有类继承。有关字符串连接和转换的更多信息,请参阅 Gosling、Joy 和 Steele 的《Java 语言规范》。

It works for primitive wrappers like Integer because of autoboxing.

It works for String because that's a special case for concatenating strings:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

幽梦紫曦~ 2024-11-04 09:37:50

+ 是一个内置操作。这是一个例外,而不是规则。

+ is a built-in operation. It's an exception, not a rule.

千年*琉璃梦 2024-11-04 09:37:50

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.

缪败 2024-11-04 09:37:50

Java 语言为字符串连接运算符 (+) 以及将其他对象转换为字符串提供了特殊支持。

String s = "string 1" + "string 2";

实际上执行的是

(new StringBuilder()).append("string 1").append("string 2").toString()

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.

String s = "string 1" + "string 2";

What actually is execute is

(new StringBuilder()).append("string 1").append("string 2").toString()
╰沐子 2024-11-04 09:37:50

正如@yan 所说,这是例外,而不是规则。字符串在 Java 中具有特殊的地位。 Java 语言规范中有一个完整的小节专门介绍了 + 作为字符串连接运算符的作用:§15.18.1

关于您的更新,这是另一个特殊情况。 Java有时,根据具体情况,足够聪明,可以在 String 时将非 String 的内容转换为 String需要代码>。这些特殊情况之一就是您所描述的情况,其中基元出现在需要 String 的位置。基元首先被转换为其引用类型 - IntegerDouble 和 &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 Strings into Strings when Strings are needed. One of these special cases is the one you described, where primitives are showing up in a place that needs a String. The primitives are first converted to their reference types — Integer, Double, &c. — and then into Strings via the toString() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文