Java: Could you explain this simple statement (System.out.println)?

Well, it's a thing called order of operations.

1 + 2 is calculated to equal 3 and then the string "3" is appended to it converting the first 3 to a string and printing "33".

In your second instance, "1" is already a string so adding numbers will convert them to strings to match, so appending "2" and then appending "3" and printing "123".

P.S. Strings take precedence because they have a higher casting priority than integers do, therefore it will convert integers to strings but not strings to integers, as with the second example.


The first statement adds 1 and 2 (since both are Integers) and then converts them to a string and appends the string "3".

The second statement has a string "1" and converts all following arguments to strings as well. So you get 123.

Tags:

Java