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

Discussion: Java convert an Int to a String

In the previous quiz, Online Java Quiz, we tested our experience gained from the course.

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
Avatar
Robin Pon
Member
Avatar
Robin Pon:20. September 16:38

Finding stylish sofas in Atlanta can be challenging, but when I found Italian Interiors, it was like striking gold https://italian-interiors.com/…nals+atlanta Their range of Italian-made sofas is incredible, with various options in terms of materials and designs. I bought a stunning leather sofa that not only looks amazing but is incredibly comfortable too. The quality and attention to detail make all the difference. If you're in Atlanta, this store is a hidden gem for sofa shopping.

 
Up Reply
20. September 16:38
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

4 messages from 4 displayed.