Hello guys, this is the second part of the 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 that directly converts an Integer object to a String like the 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 a 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 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 requires 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 issues 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 applications. 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.
You can further see the Complete Java Masterclass course on Udemy to learn more about String and Integer data types in Java. The course is now available for just $10 on flash sale which is seriously cheap.
Good knowledge of Java data types is important to write Java programs, both easy and tough and that's why I suggest you 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 requires 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 issues 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 applications. 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 Examples to convert Integer to String in Java
Following are three main ways to convert an Integer to String in Java:- Integer.toString()
- String concatenation
- String.format()
Solution 1 - Integer.toString() in Java
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(); // NPESo, make sure you do a null check before calling the toString() method on the Integer object. Remember, we are dealing with the object here and not primitive int, which can never null.
You can further see the Complete Java Masterclass course on Udemy to learn more about String and Integer data types in Java. The course is now available for just $10 on flash sale which is seriously cheap.
Solution 2 - String concatenation Example
This is an indirect way to call the 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 works out String concatenation and how the Java compiler helps there.
Solution 3 - String.format
This is the third example of converting an Integer to a String in Java, but unlike the 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.
You can see, here we need to pass an int variable but now since we have an Integer object, it needs autoboxing.Integer i = new Integer(3); String s = String.format("%d", i); System.out.println(s); // 3
Here is a nice summary of all three examples of converting Integer to String in Java:
That's all about how to convert an Integer to a 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 the Integer object with an 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.
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 the following Java programming tutorials:
- How to convert String to Double in Java? (example)
- How to convert char to String in Java? (tutorial)
- 10 Free Courses to learn Data Structure in Java (free data structure courses)
- 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 into long in Java? (answer)
- 10 Free Java Courses for Beginners to Join (free Java courses)
What about String.valueOf()?
ReplyDeleteHello Dawid, yes you can also use String.valueOf() to convert Integer to String, but mindful that's also involves auto-boxing.
DeleteIn Solution 2 - String concatenation, you wrote:
ReplyDeleteInteger 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);