Which would be the "best" way to convert this to a string:
I have tried searching (googling) for an answer but no many seemed "trustworthy".
- 7 Answers 7
- Convert using Integer.toString(int)
- Convert using String.valueOf(int)
- Convert using new Integer(int).toString()
- Convert using String.format()
- Convert using DecimalFormat
- Convert using StringBuffer or StringBuilder
- Quick Solution
- Convert with special radix
- String To Int Java
- Algorithm for converting string to int without built-in method:
- Integer.decode() – How It Works
- string to int java – Integer.valueOf()
7 Answers 7
There are multiple ways:
- String.valueOf(number) (my preference)
- "" + number (I don’t know how the compiler handles it, perhaps it is as efficient as the above)
Integer class has static method toString() — you can use it:
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
This tutorial will show example codes on how to convert Java Int to String. Performing conversion from int to String is a common scenario when programming with Core Java.
Here are some quick reference example codes:
Convert using Integer.toString(int)
The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.
The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.
The code is equivalent to:
If you will try and search for solutions, this is one of the most popular ways of converting an int to String.
Convert using String.valueOf(int)
The String class has several static methods that can convert most primitive types to their String representation. This includes integers.
This is also an efficient solution like the first option above. And because this is simple and efficient, it is also a very popular method for converting an int to String.
Convert using new Integer(int).toString()
Another alternative method is to create an instance of Integer class and then invoke it’s toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Integer is created before conversion is performed.
We can shortened to:
If your variable is of primitive type (int), it is better to use Integer.toString(int) or String.valueOf(int). But if your variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke it’s toString() method as shown above.
Convert using String.format()
String.format() is a new alternative that can be used for converting an Integer to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.
There are many options on how to use this method. But for conversion purposes, here is a simple example:
And the variable numberAsString will have the value "-782"
If you are using Java 5 or higher, this is also a very simple alternative for converting an int to String object.
Convert using DecimalFormat
The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern.
This is an example that will convert to String but with commas
This is my favorite from all the options shown in this post because of the level of control that you can do with the formatting. You can specify the number of decimal places and comma separator for readability.
Convert using StringBuffer or StringBuilder
StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer. These two classes can be used to convert a Java Integer to String.
When you concatenate a String and an integer, the result is a String. Internally, the code is inefficient because there are intermediate objects that are created behind the scene.
Convert with special radix
All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java Integer to String but using another base. There are convenient methods to convert to binary, octal, and hexadecimal system. Arbitrary custom number system is also supported.
The output is
11111111 is the binary representation of the number 255.
The output is
377 is the octal representation of the number 255.
The output is
ff is the hexadecimal representation of the number 255.
We can use any other custom base/radix when converting an int to String. The sample shown below uses the base 7 number system.
The output is
513 is the representation of the number 255 when written in the base 7 system.
in Java Tutorials Comments Off on Convert String To int Java – Tutorial & Examples
How to Convert String to int in Java – The following article will let you know the complete information about string to int java, in case if you have any doubts do let me know.
We have different ways to convert a String to int in Java
Our own logic equivalent to – Integer.parseInt()
String To Int Java
Integer.parseInt() – How It Works:
Syntax1 – int parseInt(String);
It converts the given String into an integer. Suppose we give a String “234” as argument to the method, then it returns the integer 234. As this is a static method, we can invoke this using its class name.
Algorithm for converting string to int without built-in method:
Integer.decode() – How It Works
An empty String or a String that contains non-digits will result in Number Format Exception.
“234A”, “34.54”, “smile”, “”, etc.
Syntax2: int parseInt(String,int);
This is similar to normal parseInt(), but it takes an integer as second argument and this argument represents the radix of the content in the input String.
Suppose we want to convert an octal number (which is in String format) into its equivalent decimal integer then the second argument should be 8.
Similarly, if we want a hexa-decimal String to its decimal number then the second argument should be 16.
Eg: Integer.parseInt(“234”,10); will result in 234
Integer.parseInt(“234”,2); will result in NumberFormatException as allowed digits in binary(2) number system are 0 and 1 only.
Integer.parseInt(“789”,8); will result in NumberFormatException as allowed digits in octal(8) number system are from 0 to 7 only
string to int java – Integer.valueOf()
It can also be used similar to Integer.parseInt() to convert a String to int. This method also has an overloaded method to take an int (radix) as second argument where as the first argument is a String.
Integer.parseInt() returns the result as a basic (primitive) int type where as Integer.valueOf() returns the result an Integer object.
- Integer.valueOf(“234”); will give 234 as Integer object
- Integer.valueOf(“412”); will give 412 as Integer object
- Integer.valueOf(“234”,8); will give 156 as Integer object
- Integer.valueOf(“234”,16); will give 564 as Integer object
This is also similar to Integer.valueOf() and Integer.parseInt() but it can work on positive values only.
- Integer.parseUnsignedInt(“874”); returns 874
- Integer.parseUnsignedInt(“+874”); returns 874
- Integer.parseUnsignedInt(“-874”); raises NumberFormatException
Syntax: Integer decode(String);
This method converts the given string into an int and returns an Integer object.