Java int to String - Integer.toString(i) vs new Integer(i).toString()
Solution 1:
Integer.toString
calls the static method in the class Integer
. It does not need an instance of Integer
.
If you call new Integer(i)
you create an instance of type Integer
, which is a full Java object encapsulating the value of your int. Then you call the toString
method on it to ask it to return a string representation of itself.
If all you want is to print an int
, you'd use the first one because it's lighter, faster and doesn't use extra memory (aside from the returned string).
If you want an object representing an integer value—to put it inside a collection for example—you'd use the second one, since it gives you a full-fledged object to do all sort of things that you cannot do with a bare int
.
Solution 2:
new Integer(i).toString()
first creates a (redundant) wrapper object around i
(which itself may be a wrapper object Integer
).
Integer.toString(i)
is preferred because it doesn't create any unnecessary objects.
Solution 3:
Another option is the static String.valueOf
method.
String.valueOf(i)
It feels slightly more right than Integer.toString(i)
to me. When the type of i changes, for example from int
to double
, the code will stay correct.