Concatenation of strings and characters

The following statements

String string = "string"; string = string +((char)65) + 5; System.out.println(string); 

Output stringA5 .




Further,

 String string = "string"; string += ((char)65) + 5; System.out.println(string); 

string70 .

Where is the difference?

+55
java string string-concatenation
Dec 11 '13 at 9:54 on
source share
5 answers

You see this behavior as a result of a combination of operator priorities and string conversions.

JLS 15.18.1 states:

If only one operand expression is of type String, then string conversion (ยง5.1.11) is performed on the other operand to create the string at run time.

Therefore, the right operands in your first expression are implicitly converted to a string: string = string + ((char)65) + 5;

For the second expression, however string += ((char)65) + 5; , the assignment operator += should be considered along with + . Since += weaker than + , the + operator is computed first. There we have char and int, which leads to binary numeric advertising before int . Only then += is evaluated, but at this time the result of the expression including the + operator has already been evaluated.

+69
Dec 11 '13 at 10:35
source share

Case 1

 string = string +((char)65) + 5; 

everything is considered as String, but in the second case

The sequence of operations:

  • string +((char)65 = stringA
  • stringA + 5 = stringA5

Case 2

  string += ((char)65) + 5; 

the first right side is calculated means that the first operation will look like ((char)65) + 5 , so the result is ((char)65) + 5 is 70 and after that + = operation.

The sequence of operations:

  • (char)65 + 5 = 70
  • string + 70 = string70

Let's see another example

 String string = "string"; string += ((char)65) + 5 + "A"; System.out.println(string); 

Output string70A

Cause The same first right-hand side is calculated and the sequence of the operation being performed is performed.

  • (char)65 + 5 = 70
  • 70 + "A" = 70A
  • string + 70A = string70A
+16
Dec 11 '13 at 9:57
source share

When you write:

 string = string + ((char)65) + 5; 

This is similar to the entry:

 String string = new StringBuilder(string).append((char)65).append((int)5).toString(); 

This is similar to adding string to A (representing a decimal char of 65) and 5 .

In the latter, you first calculate the right hand, and then add the result to string , this is like a record:

 string = string + 70; 
+5
Dec 11 '13 at 9:58
source share
 string = string +((char)65) + 5; 

This means "Set string to concatenation of string , ((char)65) and 5 " This evaluates from left to right (first string + ((char)65) , then +5 , and concatenating the string and integer converts that integer into a string.

 string += ((char)65) + 5; 

This means "Calculate the result ((char)65)+5 and add it to the string" - the entire right-hand side is evaluated before adding the result to the string. Since a char is actually just a 16-bit integer, it concatenates them as integers, giving 70, and then adds this to the string .

+2
Dec 11 '13 at 9:59
source share

This is the same case as the example:

 System.out.println("abc"+10 + 5); 

creates abc105 (add as String)

and

 System.out.println(5 + 10); 

prints 15 (add as number)

String included in the operation (first place) causes all elements to be treated as Strings during the operation. In your case, with += however, the operation is performed first on the right side (processing elements as int ) due to the priority of the operator, and then merged with String .

0
Dec 11 '13 at 10:01
source share



All Articles