Domanda

How does the expressions evaluated inside this method. e.g System.out.println("Result"+2+3+4) yields Result234 but System.out.println("Result"+2+3*5) returns Result215

I understand that expressions are evaluated from left to right and if one operand is String, then the result will be String. For the second example above, it makes sense. But for example1 above should not the result be Result27 ?

More examples will be helpful.

È stato utile?

Soluzione

The String concatenation operator (+) triggers the need for String conversion of the int value because the first argument on the left is a String. Order of precedence causes the multiplication of the two ints to occur first in the second example before the concatenation operator is evaluated.

I'd need to check the bytecode but I believe the compiler uses a StringBuilder here so the real code would look like:

System.out.println(new StringBuilder("Result").append(2).append(3).append(4));
System.out.println(new StringBuilder("Result").append(2).append(3 * 5));

Edit for completeness:

Your examples, because they are made up entirely of literals (String and primitive), are dealt with at compile time and simply turned into a string literal. This is a Compile Time Constant Expression. Precedence still applies so the result is the same.

If any of those were to have been a variable, for example:

int x = 5;
System.out.println("Result"+2+x);

then a StringBuilder is used at runtime to concatenate the literal part with the variable's value.

(Edited to be authoritative after looking at the bytecode and JLS)

Altri suggerimenti

This is happening because * has a higher priority than +

So in this: System.out.println("Result"+2+3*5)

  1. 3*5 is calculated which results 15 and then
  2. concatenation done.

So you are getting 215

Operator precedence

System.out.println("Result"+2+3+4) yields Result234 
Reason : "Result" + "2" +"3" +"4"  // "+" is concatenation operator here


but System.out.println("Result"+2+3*5) returns Result215
Reason : "Result" +"2" +(3*5) / '*' priority is more than that of '+'.
          "Result"+"2"+"15"
          "Result215"

  System.out.println(2+3+4);
  O/P : 9 (2,3 and 4 are parsed as integers..) 

Like with normal math, there is some ordering on execution of infix symbols. At school I learnt BIDMAS or BODMAS (depending on who was teaching it) although you may also know of PEDMAS. Java follows the same order of execution for the infix symbols as mathematics (see this refference).

This means that the * symbol is evaluated before the + symbol as it has a higher priority (or more technically accurate to programming, a higher precedence). When using infix notation, java typically uses the type of the first element as the type for the output, so a + b will yeild a result of the same type as a. In this way, this is why you get string concatenation with your first example.

When java evaluates, it will group the infix notation into pairs and use this same rule so:

System.out.println("Result"+2+3*5)

becomes:

System.out.println((("Result"+2)+(3*5)))

If you pay careful attention to the brackets I have put in, you can see the order of execution quite clearly. Either the concatenation of "Result" and 2 is done first or the multiplication of the 3 and 5 - either way this would not affect the result. After this, Result2 is concatenated with 15.

If for example, you wanted to force order of execution, then brackets rule over all others so you could easily make your first example output Result9 by the following addition of brackets:

System.out.println("Result"+(2+3+4))

The safest thing to do however, is always use brackets, that way there will be no discrepancy as to what the order of evaluation is.

For a full guide on Operator Precedence you can view the Java Documentation on Operators

This happens because the println argument starts with a string, so it starts concatenating the rest of the argument as a string. But since '*' has higher priority than '+' it will evaluate the multiplication before concetenating it to the rest of the string. That is why you get 215.

If you had tried

System.out.println(2+3*5);

Then the result would be 17.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top