Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

As we know, conversion from int to String is a very common operation in 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.

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:

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

3. The String.valueOf() method

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.

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:

@Test
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. 

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.

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

@Test(expected = NullPointerException.class)
public void whenNullIntegerObjectIsPassed_thenShouldThrowException() {
    Integer i = null; 
    System.out.println(String.valueOf(i)); 
    System.out.println(i.toString());
}

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.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!