Get up to 80 % extra points for free! More info:

Discussion: Java convert an Int to a String

Activities
Avatar
tombezlar
Member
Avatar
tombezlar:3/24/2021 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
3/24/2021 6:29
Avatar
Replies to tombezlar
David Capka Hartinger:3/25/2021 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 3/25/2021 6:04
Up Reply
3/25/2021 6:03
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Rahul Kumar
Member
Avatar
Rahul Kumar:12/24/2021 2:30

Hello,
Thanks for sharing this information.
Java Course in Pune.

Edited 12/24/2021 2:31
 
Up Reply
12/24/2021 2:30
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

3 messages from 3 displayed.