Black Friday is here! Take advantage of this unique opportunity and get up to 80% extra knowledge for free! Read more here
bf extended_2022

Discussion: Java convert an Int to a String

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

3/24/2021 6:29
David Capka team
Replies to tombezlar
David Capka: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.
Rahul Kumar
Rahul Kumar:12/24/2021 2:30

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.