Java - How to convert from Integer to String?

This is the second part of data conversion tutorial, in the first part you have learned how to convert String to Integer and in this article, you will learn the opposite i.e. convert from Integer to String. Actually, you can apply all the tricks, which I had told you before about converting long to String, and autoboxing will take care of converting int to Integer in Java. But, if you care for performance and believe in not using auto-boxing when not needed then there are still a couple of ways which directly converts an Integer object to String e.g. Integer.toString() method, which returns a String object and doesn't have any auto-boxing overhead. Let's see a couple of more ways to convert an Integer to String in Java. Btw, if you are new to Java and just started learning, I suggest you to buy the Head First Java, 2nd Edition, one of the best books to learn Java.



3 ways to convert Integer to String in Java

Following are three ways to convert an Integer to String in Java:
  1. Integer.toString()
  2. String concatenation
  3. String.format()



Solution 1 - Integer.toString()

This is the best and straightforward way to convert an Integer to String object in Java. It doesn't require any auto-unboxing etc, but it will throw NullPointerException if Integer is null as shown below:
Integer i = new Integer(10);
String notNull = i.toString();

i = null;
String nullString = i.toString(); // NPE
So, make sure you do a null check before calling toString() method on Integer object. Remember, we are dealing with the object here and not primitive int, which can never null.


Solution 2 - String concatenation

This is an indirect way to call toString() method of Integer class but unlike previous method this one doesn't throw NullPointerException even if Integer is null, instead it will create a String with literal value null, as shown below :

Integer i = new Integer(10);
String s = i.toString();
s = "" + i;
System.out.println(s);


i = null;
s = "" + i;

System.out.println(s);

Remember, String concatenation is replaced with StringBuilder append() call internally.


Solution 3 - String.format

This is the third example of converting an Integer to String in Java, but unlike other two this involves autoboxing. As shown in the earlier article about converting int to String, you can pass an int primitive when you use %d as format instruction, and this result in String equivalent of integer passed as shown below.
Integer i = new Integer(3);
String s = String.format("%d", i);
System.out.println(s); // 3
You can see, here we need to pass an int variable but now since we have an Integer object, it needs autoboxing.

Here is the nice summary of all three examples of converting Integer to String in Java:

3 examples to convert Integer to String in Java


That's all about how to convert an Integer to String in Java. As you have seen, the most straightforward way is to use Integer.toString() method, it doesn't need autoboxing and it's direct way to get String from Integer. Alternatively you can concatenate Integer object with empty String. This will internally call the toString() only. The third way is just for information purpose though it useful, but its purpose is different. You should use String.format() to produce a formatted String not to convert an Integer to String.


Related Java Tutorials
If you like this tutorial and wants to learn more about how to convert one data type into another, Please check out following Java programming tutorials:
  • How to convert String to Double in Java? (example)
  • How to convert char to String in Java? (tutorial)
  • How to convert Enum to String in Java? (example)
  • How to convert ByteBuffer to String in Java? (program)
  • How to convert String to Date in a thread-safe manner? (example)
  • How to convert float to String in Java? (example)
  • How to convert byte array to String in Java? (program)
  • How to convert double to Long in Java? (program)
  • How to convert String to int in Java? (example)
  • How to convert Decimal to Binary in Java? (example)
  • How to parse String to long in Java? (answer)


Further Reading
If you have just started learning Java then you need a book. Head First Java is the best book to start with but if you find it little comic and interested in real book then read Core Java 9th Edition by Cay S. Horstman, the second best book to learn Java.


3 comments:

  1. Replies
    1. Hello Dawid, yes you can also use String.valueOf() to convert Integer to String, but mindful that's also involves auto-boxing.

      Delete
  2. In Solution 2 - String concatenation, you wrote:

    Integer i = new Integer(10);
    String s = i.toString();
    s = "" + i;
    System.out.println(s);

    It should be:
    Integer i = new Integer(10);
    String s = "" + i;
    System.out.println(s);

    ReplyDelete