Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String Concatenation with + operator

Tags:

java

string

I got confused with the String concatenation.

String s1 = 20 + 30 + "abc" + (10 + 10);
String s2 = 20 + 30 + "abc" + 10 + 10;
System.out.println(s1);
System.out.println(s2);

The output is:

50abc20
50abc1010

I wonder why 20 + 30 are added together in both cases, but 10 + 10 require parenthese in order to be added (s1) instead of concatenated to the String (s2). Please explain how the String operator + works here.

like image 314
shekhar Avatar asked Mar 11 '13 13:03

shekhar


People also ask

How string concatenation using the operator works in Java?

Using the + operator is the most common way to concatenate two strings in Java. You can provide either a variable, a number, or a String literal (which is always surrounded by double quotes). Be sure to add a space so that when the combined string is printed, its words are separated properly.

How is string concatenation done using operator?

Using + Operator It's very easy to use + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string. Note: Strings are immutable, therefore, whenever it is concatenated, it is assigned to a new variable.

Which operator can be used in string concatenation Java?

In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

Can you use += for string concatenation?

The same + operator you use for adding two numbers can be used to concatenate two strings. You can also use += , where a += b is a shorthand for a = a + b .


3 Answers

Addition is left associative. Taking the first case

20+30+"abc"+(10+10)
-----       -------
  50 +"abc"+  20    <--- here both operands are integers with the + operator, which is addition
  ---------
  "50abc"  +  20    <--- + operator on integer and string results in concatenation
    ------------
      "50abc20"     <--- + operator on integer and string results in concatenation

In the second case:

20+30+"abc"+10+10
-----
  50 +"abc"+10+10  <--- here both operands are integers with the + operator, which is addition
  ---------
   "50abc"  +10+10  <--- + operator on integer and string results in concatenation
    ----------
    "50abc10"  +10  <--- + operator on integer and string results in concatenation
     ------------
      "50abc1010"   <--- + operator on integer and string results in concatenation
like image 76
uba Avatar answered Oct 20 '22 00:10

uba


Adding to the concept of associativity, you could ensure that two integers are never added together by using parentheses to always pair a string with an integer so the desired concatenation operation will take place rather than an addition.

String s4 = ((20 + (30 + "abc")) + 10)+10;

would produce:

2030abc1010
like image 37
MFarmer Avatar answered Oct 20 '22 01:10

MFarmer


Also, to add to this topic, as the wrong part of the answer of Jonathan Schober tipped me off on a thing to keep in mind:

a+=something is not equal to a=a+<something> : the += evaluates the right side first, and only then adds it to the left side. So it has to be rewritten, it is equivalent to:

a=a+(something); //notice the parentheses!

Showing the difference

public class StringTest {
  public static void main(String... args){
    String a = "";
    a+=10+10+10;

    String b = ""+10+10+10;

    System.out.println("First string, with += : " + a);
    System.out.println("Second string, with simple =\"\" " + b);

  }
}
like image 41
ppeterka Avatar answered Oct 20 '22 00:10

ppeterka