+ operator for String in Java [duplicate]

霸气de小男生 提交于 2019-12-28 22:12:11

问题


I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the + operator.

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?


回答1:


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(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.

See String Concatenation in the JLS.




回答2:


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:

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.




回答3:


It is handled by the compiler.




回答4:


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.




回答5:


Most of the answers here are correct (it's handled by the compiler, + is converted to .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.

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

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.

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.

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.




回答6:


It's done at the language level. The Java Language Specification is very specific about what string addition must do.




回答7:


String is 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++).

Interestingly enough: This question was logged as a bug: http://bugs.sun.com/view_bug.do?bug_id=4905919



来源:https://stackoverflow.com/questions/2328483/operator-for-string-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!