Home > DeveloperSection > Forums > How to convert from int to String in java?
naomi burke
naomi burke

Total Post:28

Posted on    August-04-2015 2:34 AM

 Java Java  String 

 1 Reply(s)
 676  View(s)
Rate this:
I'm working on a project where all conversions from int to String are done like this:
int i = 5;
String strI = "" + i;
I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose?

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    August-04-2015 7:06 AM

1. Integer.toString(i)
2.  String.valueOf(i)
The concatenation will work, but is unconventional and could be a bad smell as it suggests the author doesn't know about the two methods above (what else might they not know?).

Java has special support for the + operator when used with strings (see docs) which translates the code you posted into:
StringBuilder sb = new StringBuilder();
String strI = sb.toString();

Don't want to miss updates? Please click the below button!

Follow MindStick