Integer.toString() vs String.valueOf() in Java – Java中Integer.toString() vs String.valueOf()

最后修改: 2022年 8月 17日

1. Overview


As we know, conversion from int to String is a very common operation in Java.

正如我们所知,从int String 的转换是Java中非常常见的操作。

In this short tutorial, we’ll go through two very popular methods, toString() of the Integer class and valueOf() of the String class, which helps us with this conversion. Moreover, we’ll also look at a few points and examples using these two methods to understand it better.

在这个简短的教程中,我们将通过两个非常流行的方法,类的Integer toString()类的valueOf(),帮助我们进行这种转换。此外,我们还将看一下使用这两种方法的几个要点和例子,以便更好地理解它。

2. The Integer.toString() method


This method accepts an integer of primitive data type int as a parameter and returns a String object representing the specified integer.


Let’s look at its signature:


public static String toString(int i)

Now, we’ll see a few examples where we pass signed/unsigned integers as parameters to it to understand integer to string conversion happens:


public void whenValidIntIsPassed_thenShouldConvertToString() {
    assertEquals("11", Integer.toString(11)); 
    assertEquals("11", Integer.toString(+11)); 
    assertEquals("-11", Integer.toString(-11));

3. The String.valueOf() method

3.String.valueOf() 方法

This method also accepts an integer of primitive data type int as a parameter and returns a String object. Interestingly, the returned String representation is exactly the same as the one returned by the Integer.toString(int i) method. It is because internally, it uses Integer.toString() method.

这个方法也接受一个原始数据类型int的整数作为参数并返回一个String对象。有趣的是,返回的字符串表示法与Integer.toString(int i)方法返回的完全一样。这是因为在内部,它使用了Integer.toString()方法。

Let’s look at its internal implementation as given in java.lang.String class:


 * 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);

To understand it better, we’ll see a few examples where we pass signed/unsigned integers as parameters to it to understand integer to string conversion happens:


public void whenValidIntIsPassed_thenShouldConvertToValidString() {
    assertEquals("11", String.valueOf(11)); 
    assertEquals("11", String.valueOf(+11));
    assertEquals("-11", String.valueOf(-11));

4. Differences Between Integer.toString() and String.valueOf()


To summarize, there are no actual differences between these two methods, but we should understand the following points to avoid confusion.


There is one extra call in the stack trace when we use String.valueOf() method because it internally uses the same Integer.toString() method.


There can be some confusion when passing a null object to valueOf() method because, when passing a primitive int to valueOf() method as it looks the same, but the actual method call goes to a different overloaded method. 

当把一个null对象传递给valueOf()方法时,可能会有一些混淆,因为 当把一个原始的int传递给valueOf()方法时,因为它看起来是一样的,但是实际的方法调用是到一个不同的重载方法。

Integer.toString() might throw NullPointerException if given Integer object is null. String.valueOf() won’t throw an exception because it’ll go to String.valueOf(Object obj) method and return null. Please note, primitive int passed to String.valueOf(int i) can never be null but since there is another method String.valueOf(Object obj), we can get confused between the two overloaded methods.

Integer.toString()可能会抛出NullPointerException,如果给定的Integer对象是nullString.valueOf()不会抛出异常,因为它会进入String.valueOf(Object obj) 方法并返回null请注意,传递给String.valueOf(int i)primitive int永远不可能是null,但由于有另一个方法String.valueOf(Object obj),我们会在这两个重载方法之间混淆。

Let’s understand the last point with the following example:


@Test(expected = NullPointerException.class)
public void whenNullIntegerObjectIsPassed_thenShouldThrowException() {
    Integer i = null; 

Please note that primitive int can never be null, we’re checking it in case the exception is thrown by methods below it.


5. Effect of JVM Method Inlining on String.valueOf()  method


As we discussed earlier, String.valueOf() method involves an extra call. But, JVM can eliminate even that additional call in the stack trace by method inlining.


However, this depends entirely on whether JVM chooses to inline that method. For a more detailed description, please visit our article on Method Inlining in the JVM.


6. Conclusion


In this article, we learnt about the Integer.toString() and String.valueOf() methods. We also looked at a few points where we should concentrate to avoid confusion while programming.


As always, the complete code samples for this article can be found over on GitHub.