Concatenation Surprise
Completed
Print the following three expressions, one per line: "Sum: " + 2 + 3, "Sum: " + (2 + 3) and 2 + 3 + " apples".
Input
None.
Output
Three lines.
Example
Output
Sum: 23 Sum: 5 5 apples
Editor
Java
Hints
+ is evaluated left to right.
Once one side is a String, + joins text instead of adding.
Solution explanation
"Sum: " + 2 is already a String, so the 3 is appended as text. Parentheses force the addition first.
Show solution
public class Main {
public static void main(String[] args) {
System.out.println("Sum: " + 2 + 3);
System.out.println("Sum: " + (2 + 3));
System.out.println(2 + 3 + " apples");
}
}