Discussion: Java convert an Int to a String

tombezlar:24. March 6:29

Here are 3 ways to convert an int to a String

String s = String.valueOf(n);
String s = Integer.toStrin­g(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

24. March 6:29
David Capka
ICT.social team
Replies to tombezlar
David Capka:25. March 6:03

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.

Edited 25. March 6:04
Up Reply
25. March 6:03
You can walk through a storm and feel the wind but you know you are not the wind.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

2 messages from 2 displayed.