shekhar
shekhar

Reputation: 95

Java String Concatenation with + operator

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.

Upvotes: 8

Views: 3520

Answers (5)

uba
uba

Reputation: 2031

+ can represent addition or concatenation.

  • If both operands are numeric it is addition.
  • If at least one operand is String it is concatenation.

Also addition and concatenation is left associative, so a+b+c is same as (a+b)+c (the b is associated with left +).

Taking the first case

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

In the second case:

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

Upvotes: 12

ppeterka
ppeterka

Reputation: 20736

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);

  }
}

Upvotes: 2

Mark Yao
Mark Yao

Reputation: 408

You need to know some rules:
1, Java operator priority,most of the left-to-right
2, Brackets priority than + sign priority.
3, The result is sum,if both sides of + sign are integer, else is concatenation.

Upvotes: 0

Jonathan Schober Jr.
Jonathan Schober Jr.

Reputation: 55

You will need to start with an empty string.

So, this might work:

String s2 = ""+20+30+"abc"+10+10; 

Or this:

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

Upvotes: 0

MFarmer
MFarmer

Reputation: 1716

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

Upvotes: 1

Related Questions