How to convert Integer to String in Java - Example

This is the second part of String to Integer data type conversion tutorial in Java, 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.

Good knowledge of Java data types is important to write Java programs, both easy and tough and that's why I suggest you to spend some time learning about data types in Java. Even though choosing the right data type for the task is a little bit tough and require some experience, you must first know what are your options are.

If you know about all data types in Java, you are more likely to choose the right data type. Choosing the wrong data type can result from an incorrect program and subtle issue especially in the real world application where production will have a lot of data.

One example of this is choosing int data type to represent an id rather than long because the range of integer is not enough for many real-world Java application. Similarly, if you can live with float then using double will cost you 4 extra bytes which can be a lot of memory if you multiply with the number of times you use it.

Btw, if you are new to Java and just started learning, I suggest you join the Complete Java Masterclass course on Udemy which is both comprehensive and up-to-date. It is also recently updated for Java 11.





3 ways to convert Integer to String in Java

Following are three main ways to convert an Integer to String in Java:
  1. Integer.toString()
  2. String concatenation
  3. String.format()
there are a couple of other ways but these three would be more than enough to complete the task, hence we'll only focus on these three examples.

Solution 1 - Integer.toString()

This is the best and straightforward way to convert an Integer to a 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.

You can further see Complete Java Masterclass course on Udemy to learn more about String and Integer data type in Java. The course is now available in just $10 on flash sale which is seriously cheap.

Integer to String in Java - Example



Solution 2 - String concatenation

This is an indirect way to call toString() method of Integer class but unlike the 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. You can also go through the Java Fundamentals: The Java Language to learn more about how JVM work out String concatenation and how Java compiler helps there.

How to convert from Integer to String in Java

Btw, you would need a Pluralsight membership to get access this course, which cost around $29 per month or $299 annually (14% discount).

If you don't have Pluralsight membership, I encourage you to get one because it allows you to access their 5000+ online courses on all latest topics like front-end and back-end development, machine learning etc.  It also includes interactive quizzes, exercises, and latest certification material.

It's more like Netflix for Software Developers and Since learning is an important part of our job, Plurlasight membership is a great way to stay ahead of your competition.

They also provide a 10-day free trial without any commitment, which is a great way to not just access this course for free but also to check the quality of courses before joining Pluralsight.


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 an int value 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 a 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, its purpose is different. You should use String.format() to produce a formatted String not to convert an Integer to String.


Further Learning
The Complete Java Masterclass
Java Fundamentals: The Java Language
Data Structures and Algorithms: Deep Dive Using Java


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)

Thanks a lot for reading this article so far. If you like this tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. If you like to stay updated, you can also follow me on twitter, my id is @javinpaul.

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