0

Normally I used one of these methods while converting an integer to a string:

  • Integer.toString(i) or
  • String.valueOf(i) while i is an integer-value.

Are both ways are correct?

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 1
    What makes you feel it might not be correct. Does it sometimes fail, or do something suprising? Have you read the documentation? – JB Nizet Dec 29 '15 at 13:26
  • You should do the one which is more obvious to you. I use `"" + i` which is shorter, though slower. – Peter Lawrey Dec 29 '15 at 13:29

1 Answers1

6

Both ways are the same :

/**
 * Returns the string representation of the {@code int} argument.
 * <p>
 * The representation is exactly the one returned by the
 * {@code Integer.toString} method of one argument.
 *
 * @param   i   an {@code int}.
 * @return  a string representation of the {@code int} argument.
 * @see     java.lang.Integer#toString(int, int)
 */
public static String valueOf(int i) {
    return Integer.toString(i);
}
Eran
  • 387,369
  • 54
  • 702
  • 768