Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation of Strings and characters

The following statements,

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

Produce the output stringA5.


The following however,

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

Produce string70.

Where is the difference?

like image 942
Tiny Avatar asked Dec 11 '13 09:12

Tiny


People also ask

Can we concatenate string and character?

Concatenating strings would only require a + between the strings, but concatenating chars using + will change the value of the char into ascii and hence giving a numerical output.

Which character is used for concatenation of strings?

The & character is used to concatenate, or join, two or more strings or the contents of referenced cells. Some examples of the use of the concatenation operator are: "Abc"&"Def" returns "AbcDef".

Can we concatenate string and character in Java?

String concatenation using StringBuilder class The append() method accepts arguments of different types like Objects, StringBuilder, int, char, CharSequence, boolean, float, double. StringBuilder is the most popular and fastet way to concatenate strings in Java.

What is the concatenation character?

The concatenation operator is a binary operator, whose syntax is shown in the general diagram for an SQL Expression. You can use the concatenation operator ( || ) to concatenate two expressions that evaluate to character data types or to numeric data types.


2 Answers

You see this behavior as a result of the combination of operator precedence and string conversion.

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 produce a string at run time.

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

For the second expression however string += ((char)65) + 5; the += compound assignment operator has to be considered along with +. Since += is weaker than +, the + operator is evaluated first. There we have a char and an int which results in a binary numeric promotion to int. Only then += is evaluated, but at this time the result of the expression involving the + operator has already been evaluated.

like image 88
Kai Sternad Avatar answered Oct 20 '22 01:10

Kai Sternad


Case 1

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

everything is treated as String but in second case

Sequence of operation performed:

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

Case 2

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

first right hand side is calculated means first operation will be like ((char)65) + 5, So result of ((char)65) + 5 is 70 and after that += operation.

Sequence of operation performed:

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

Lets see 1 more example

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

Output string70A

Reason Same first right hand side is calculated and sequesce of opertion performed is

  • (char)65 + 5 = 70
  • 70 + "A" = 70A
  • string + 70A = string70A
like image 26
Ashish Aggarwal Avatar answered Oct 20 '22 00:10

Ashish Aggarwal