Are you wondering how to convert int to String in Java? Lucky for you there are many ways to do it.
How to Convert Int to String in Java: All the Ways
How to convert int to String in Java? Well, there are technically several ways, and like most things, the method you need depends on what you’re trying to achieve.
There is the integer.toString(int) method, or the String.valueOf(int) technique; you can use the simpler integer(int).toString code or there’s DecimalFormat and StringBuffer/StringBuilder.
Why you need to know how to convert int to String in Java
When any coder uses Core Java for their primary programming language, how to convert int to String in Java is a common scenario. In order to create readable output, numbers classified as an integer are often required to be translated to Strings so that the data is re-classified as text.
If you’re just getting started in the programming field, you might want to check out this resource; it was beneficial for me. If you’re looking for the fastest way to learn Java, this promises to get you to your goal in a day.
Does it matter which method you choose?
As you develop your skills as a proficient coder, you will want to increase the efficiency of your code.
For that reason, some methods of how to convert int to String in Java are better than others. The best practices decrease the amount of garbage code that you’ll need to clean up afterward.
Planning the Plan
As a best practice, every coder plans out his or her code before ever actually writing a line of Java. Often you’ll be told to work out the logic of your code before you begin typing out the syntax.
Doing this will help you determine if you need to compile an int to String conversion and then, also, which method will be best.
The Simplest Method of How to Convert Int to String in Java
The Integer.toString(int) command is the simplest method to use. For this reason, it’s also the most popular. The problem with this method is that it also does an array of other executions that are not typically necessary, and for that reason, it is not the most efficient.
Here is an example of how Integer.toSting(int) works:
int number = -238;
String numberAsString = Integer.toString(number);
When Efficiency Matters
Perhaps You Should Use String.valueOf(int) as it is more efficient than the previous method. This command is also the most versatile, as it converts other primitive types to the String type as well.
Here’s an example of String.valueOf(int) syntax:
int number = -639;
String numberAsString = String.valueOf(number);
Sometimes Being Clear Is More Important
With the new Integer(int).toString() method, it is possible to convert a variable of any primitive type to an integer first. After the creation of the integer, it is converted to String. Although this is less efficient as it does create an additional operation, for anyone reviewing the Java code, this method is easier to understand.
If your goal is to print an integer, don’t use this method as the other ones described above are lighter, faster and don’t use extra memory. If instead, you are looking for an object that represents an integer value, which lets you do all sorts of things a bare integer does not, then this is the right method for you.
Here’s the example:
int number = -964;
Integer intInstance = new Integer(number);
String numberAsString = intInstance.toString();
Notice there is an extra line of code for this method.
Formatted the Way You like It
With the java.text.DecimalFormat method, you can specify the number of decimal places and even whether or not your String needs a comma. The ideal method of how to convert int to String in Java if you’re coding for financial reports or documents.
Here’s an example of the syntax that will give you a comma:
int number = 12678; DecimalFormat decimalFormat = new DecimalFormat(“#,##0”);
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Output looks like this: 12,678
With control over the formatting of the output, you can make your number look how you would expect on a balance sheet.
Let’s Mash It All Together
The StringBuffer or StringBuilder methods concatenate. Meaning this command will take integers and String values and combine them into one String value.
It’s a quick and dirty way to learn how to convert int to String in Java; however, it’s inefficient and also generates a lot of background code that slows your program down when you run it.
Here’s an example of the StringBuffer syntax:
int number = -782;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();
What About the Other Types of Systems?
Sometimes when coding, you have to deal with hard code that’s not using a base of 10. Instead, it uses binary, octal, and hexadecimal systems. If this is the case, how to convert int to String in Java may require the special radix method.
If you need to convert an integer to String using something other than the base 10, this method is for you.
Here’s an example of special radix in action for a hexadecimal system:
int number = 477;
String hexString = Integer.toHexString(number);
System.out.println(hexString);
The New Kid on the Block
The release of Java 5 (JDK1.5) came the introduction of String.format(). If you are fortunate enough to be using Java 5 or higher, than you now know how to convert int to String in Java in an incredibly simple and efficient method.
This is so simple to use:
int number = -943;
String numberAsString = String.format (“%d”, number);
All the Tools Are Now in Your Toolbox
Now you know how to convert int to String in Java in more ways than you could imagine. The method you decide on should rely on the goal you are looking to achieve with your code. That’s why it’s so important to map out your logic first before you even begin to type a line of code in your compiler.
You may very well have your preferred method to convert an integer to String in Java. If it’s not covered here, please share your thoughts in the comments section.