Reputation: 720
I was going through an AP Comp Sci practice test and found the following problem:
what is the output of:
System.out.println("1" + new Integer(2) + 3);
The answer was
123,
I am confused as the new Integer(2)
has not been casted into a String and therefore why does the java compiler believe that the new Integer(2) + 3
statement is a String concatenation if both parts are integers?
Upvotes: 3
Views: 251
Reputation: 11
The original question was:
System.out.println("1" + new Integer(2) + 3);
and why does that give us "123"; I assume the questioner meant rather than 6 or "15"?
Let's simplify this and remove the new Integer bit to its equivalent:
System.out.println("1" + 2 + 3);
The Java Language Specification 12 gives the answer (4.2.2):
The string concatenation operator + (§15.18.1), which, when given a String operand and an integral operand, will convert the integral operand to a String (the decimal form of a byte, short, int, or long operand, or the character of a char operand), and then produce a newly created String that is the concatenation of the two strings. https://docs.oracle.com/javase/specs/
The 15.18.1 section is even more clear:
The additive operators have the same precedence and are syntactically left-associative (they group left-to-right). If the type of either operand of a + operator is String, then the operation is string concatenation. https://docs.oracle.com/javase/specs/
So, since the operator + is used in both cases, it evaluates left-to-right, whether it is concatenation or addition, as stated in 15.18.1 and as other answerers have stated. The first operand "1" is a string and the second an integer 2, so according to the above rule, the integer 2 is converted to a string "2" and the plus is interpreted as concatenation, giving us a string "12". Then it has a string "12" and an integer 3, so the integer 3 is converted according to the above rule and the + is again interpreted as concatenation and we get a string "123".
If they had put parentheses around the 2 + 3:
System.out.println("1" + (2 + 3));
That, obviously, would force the 2 + 3 to be evaluated first. They are both integers, so you get an integer 5. Then we would have "1" + 5, which is a string plus an integer, so the integer is converted to a string "5" and they are concatenated, yielding "15".
If they had changed the order like this:
System.out.println(2 + 3 + "1");
Then, the 2 + 3 would be done first in accordance with the left-to-right rule, and since they are both integers, + would mean addition and that would yield an integer 5. Then we would have operators integer 5 and a string "1". According to the above rule, the integer 5 is converted to a string 5 and the + is interpreted as concatenation and we get a string "51".
So this all boils down to order of operations, the fact that all of these operations are really binary (you only take two at a time), and, when using a plus sign, if one operand is a string, the other is changed to a string if it is not already one, the plus sign is interpreted as concatenation, and the result is a string.
Upvotes: 0
Reputation: 7667
First, as the guy points out, addition is left-associative.
Second, the overload resolution of "1" + 2 is controlled by the left operand, which is a String. That forces concatenation, and the result is "12".
Now, "12" + 3 goes through the exact same overload resolution, and you get "123".
Upvotes: 0
Reputation: 2032
Is the answer as simple as order of operations meaning that the statement is evaluated left to right so it could read. System.out.println("1" + new Integer(2).toString() + 3.toString());
Upvotes: 1