Get up to 40 % extra points for free! More info
Only this week sale up to 80 % on e-learning courses of Java
discount 40

Discussion: Java convert an Int to a String

Activities (4)
Avatar
tombezlar
Member
Avatar
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

 
Reply
24. March 6:29
Avatar
David Capka
ICT.social team
Avatar
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.