Java Integrer to String Examples


Convert using Integer.toString(int)

The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.


public static String toString(int i)

The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.


int number = -782;
String numberAsString = Integer.toString(number);

The code is equivalent to:

String numberAsString = "-782";

If you will try and search for solutions, this is one of the most popular ways of converting an int to String.

Convert using String.valueOf(int)

The String class has several static methods that can convert most primitive types to their String representation. This includes integers.


int number = -782;
String numberAsString = String.valueOf(number);


String numberAsString = String.valueOf(-782);

This is also an efficient solution like the first option above. And because this is simple and efficient, it is also a very popular method for converting an int to String.

This entry was posted in Compus, General and tagged . Bookmark the permalink.