+ for String in Java

I saw this question a few minutes ago and decided to look in the Java String class to see if there was an overload for the + operator.

I could not find anything, but I know that I can do it

 String ab = "ab"; String cd = "cd"; String both = ab + cd; //both = "abcd" 

Where is this implemented?

+7
java string operator-overloading string-concatenation
source share
7 answers

From Fine Manual :

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

See String Concatenation in JLS.

+12
source share

The compiler processes your code as if you wrote something like:

 String both = new StringBuilder().append(ab).append(cd).toString(); 

Edit: Any link? Well, if I compile and decompile the OP code, I get the following:

 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, as I said.

+7
source share

It is processed by the compiler.

+2
source share

This is a special behavior described in the language specification .

15.18.1 String concatenation operator +

If there is only one expression of the operand of type String, then string conversion is performed on the other operand to output the string at run time. the result is a reference to the string object (newly created, if only the expression is the compilation time constant of the expression (Β§15.28)), that is, the concatenation of two operands of the string. The characters of the left operand are preceded by the characters of the right operand in the newly created line. If the operand of type String is null, then the string "null" is used instead of this operand.

+2
source share

Most of the answers here are correct (processed by the compiler, + converted to .append () ...)

I wanted to add that everyone should take a look at the source code for String and add at some point, this is pretty impressive.

I suppose it came down to something like:

 "a"+"b"+"c" 

=

 new String().append("a").append("b").append("c") 

But then some kind of magic happens. It turns into:

  • Create an array of strings of length 3
  • copy a to the first position.
  • copy b to second
  • copy c to third

While most people believe that it will create "ab", then throw it away when it creates "abc". He really understands that he is attached and does some manipulation.

There is also a trick where, if you have the string "abc" and you request a substring that turns out to be "bc", they can share the same base array. You will notice that there is a start position, end position and a common flag.

In fact, if it is not used, it can extend the length of the string and copy the rest.

Now I'm just confused. Read the source code - it's pretty cool.

+2
source share

This is done at the language level. The Java language specification is very specific as to what the addition of the string should contain .

0
source share

String defined as a standard type such as int, double, float, etc. at the compiler level. In fact, all compilers have operator overloading. Operator overloading is not defined for developers (unlike C ++).

Interestingly, this question was recorded as an error: http://bugs.sun.com/view_bug.do?bug_id=4905919

0
source share

All Articles