To answer your direct question: you are correct that the ""
is there to turn the +
into a concatenation, rather than addition. But I'll give a few more details as to what's going on here.
It's perhaps a little bad of the authors of the book to use this without explaining, since this appears to be a key thing in the exercise (even a brief footnote saying "this is just an easy way to convert to a String"); but it's one of those things most Java programmers wouldn't even think about needing to explain, so easy to miss; or, perhaps they did explain it earlier, and you just missed it, because of the throw-away nature of the explanation. No accusations; it's just what it is.
We're trying to evaluate the expression x + "" + y + " "
in order to print it. Because +
is left-associative, this is evaluated as
((x + "") + y) + " "
So, in terms of the effect of including ""
, the question is how the subexpression x + ""
will be evaluated. If the ""
had been omitted, it would be evaluated as
(x + y) + " "
There is no separate operator for "numeric addition" and "string concatenation" in Java: both use +
. So, Java uses a simple rule to decide between the two:
If the type of either operand of a + operator is String, then the operation is string concatenation.
As such, x + ""
is string concatenation, but x + y
would be numeric addition.
If you look into the specifics of string concatenation:
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
So this is saying that the int
in int + String
has to be converted to a string, in order to concatenate it with another string, which is pretty reasonable. And this string conversion is done using:
If T
is byte
, short
, or int
, then use new Integer(x)
.
...
the conversion is performed as if by an invocation of the toString
method of the referenced object with no arguments
("as if" is just wiggle room to say you don't actually have to invoke new Integer(x)
- implementations could use Integer.toString(x)
, or some other method, instead).
Then, concatenation is defined as:
The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
Given that ""
has no characters, it is trivially true that someString + ""
will have the same characters as someString
(*).
So, int + ""
(or "" + int
) is simply a syntactic trick to convert the int
to a String
. There is no other effect of doing this. It's just less verbose to write this than Integer.toString(x)
or new Integer(x).toString()
.
It's also worth pointing out that it would work the same had it been "" + x + y + " "
. But that's perhaps less clear to the human eye that the x
and y
won't be added.
(*) The slight detail here is that concatenation results in a new string (unless the two operands are constant expressions, in which case the concatenation is done at compile time), i.e. someString + "" != someString
, even though (someString + "").equals(someString)
.