+ Java 中字符串的运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2328483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
+ operator for String in Java
提问by Samuel Carrijo
I saw this questiona few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the +operator.
几分钟前我看到了这个问题,并决定查看java String 类以检查+操作符是否有一些重载。
I couldn't find anything, but I know I can do this
我找不到任何东西,但我知道我可以做到这一点
String ab = "ab";
String cd = "cd";
String both = ab + cd; //both = "abcd"
Where's that implemented?
在哪里实施?
回答by Josh Lee
From the Fine Manual:
从精细手册:
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(orStringBuffer) class and itsappendmethod. 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.
Java 语言为字符串连接运算符 ( + ) 以及将其他对象转换为字符串提供了特殊支持。字符串连接是通过
StringBuilder(orStringBuffer) 类及其append方法实现的。字符串转换通过toString方法实现,由Object定义,Java中所有类都继承。有关字符串连接和转换的其他信息,请参阅 Gosling、Joy 和 Steele,Java 语言规范。
See String Concatenationin the JLS.
请参阅JLS 中的字符串连接。
回答by Sean
The compiler treats your code as if you had written something like:
编译器将您的代码视为您编写了以下内容:
String both = new StringBuilder().append(ab).append(cd).toString();
Edit: Any reference? Well, if I compile and decompile the OP's code, I get this:
编辑:任何参考?好吧,如果我编译和反编译 OP 的代码,我会得到这个:
0: ldc #2; //String ab
2: astore_1
3: ldc #3; //String cd
5: astore_2
6: new #4; //class java/lang/StringBuilder
9: dup
10: invokespecial #5; //Method java/lang/StringBuilder."<init>":()V
13: aload_1
14: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
17: aload_2
18: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
24: astore_3
25: return
So, it's like I said.
所以,就像我说的。
回答by Otto Allmendinger
This is special behavior documented in the language specification.
这是语言规范中记录的特殊行为。
15.18.1 String Concatenation Operator +
If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (§15.28))that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string. If an operand of type String is null, then the string "null" is used instead of that operand.
15.18.1 字符串连接运算符 +
如果只有一个操作数表达式是字符串类型,则在运行时对另一个操作数执行字符串转换以生成字符串。结果是对 String 对象(新创建的,除非表达式是编译时常量表达式(第 15.28 节))的引用,它是两个操作数字符串的串联。在新创建的字符串中,左侧操作数的字符位于右侧操作数的字符之前。如果 String 类型的操作数为 null,则使用字符串“null”代替该操作数。
回答by Bill K
Most of the answers here are correct (it's handled by the compiler, + is converted to .append()...)
这里的大多数答案都是正确的(由编译器处理,+ 转换为 .append()...)
I wanted to add that everyone should take a look at the source code for String and append at some point, it's pretty impressive.
我想补充一点,每个人都应该看看 String 的源代码并在某个时候追加,这非常令人印象深刻。
I believe it came down to something like:
我相信它归结为:
"a"+"b"+"c"
=
=
new String().append("a").append("b").append("c")
But then some magic happens. This turns into:
但随后发生了一些神奇的事情。这变成:
- Create a string array of length 3
- copy a into the first position.
- copy b into the second
- copy c into the third
- 创建一个长度为 3 的字符串数组
- 将 a 复制到第一个位置。
- 将 b 复制到第二个
- 将 c 复制到第三个
Whereas most people believe that it will create "ab", then throw it away when it creates "abc". It actually understands that it's being chained and does some manipulation.
而大多数人认为它会创建“ab”,然后在创建“abc”时将其丢弃。它实际上知道它正在被链接并进行一些操作。
There is also a trick where if you have the string "abc" and you ask for a substring that turns out to be "bc", they CAN share the exact same underlying array. You'll notice that there is a start position, end position and "shared" flag.
还有一个技巧,如果您有字符串“abc”并且您要求一个结果为“bc”的子字符串,它们可以共享完全相同的底层数组。您会注意到有一个开始位置、结束位置和“共享”标志。
In fact, if it's not shared, it's possible for it to extend the length of a string and copy the others in.
事实上,如果它不共享,它可能会扩展字符串的长度并将其他字符串复制进去。
Now I'm just being confusing. Read the source code--it's fairly cool.
现在我只是感到困惑。阅读源代码——它相当酷。
回答by Fredrik
It is handled by the compiler.
它由编译器处理。
回答by Chris Jester-Young
It's done at the language level. The Java Language Specification is very specific about what string addition must do.
它是在语言级别完成的。Java 语言规范非常具体地说明了字符串添加必须做什么。
回答by Buhake Sindi
Stringis defined as a standard type just like int, double, float, etc. on compiler level. Essentially, all compilers have operator overloading. Operator overloading is not defined for Developers (unlike in C++).
String在编译器级别被定义为标准类型,就像 int、double、float 等。本质上,所有编译器都有运算符重载。没有为开发人员定义运算符重载(与 C++ 不同)。
Interestingly enough: This question was logged as a bug: http://bugs.sun.com/view_bug.do?bug_id=4905919
有趣的是:这个问题被记录为一个错误:http: //bugs.sun.com/view_bug.do?bug_id=4905919

