String s = String.valueOf(n);
String s = Integer.toString(n);
String s = "" + n;
I understand what we did for the first and second methods. But can someone
please explain what does 3rd method do? In other words, how does "" convert
integer into string? Thanks
The third method forces the compiler to convert n to string.
Concatenating (adding strings together by using the + operator) even an empty
string in this case with anything else will always result in a string. So the
number has to be converted to make the resulting string. Anyway, this is a weird
method to do such thing and can be misunderstood, use the 2 above.