Converting
Integer to String
The
Integer class has a static method that returns a String object representing the
specified int parameter. Using this is an efficient solution.
Syntax 
public static String toString(int i)
The
argument i is converted and returned as a string instance. If the number is
negative, the sign will be preserved.
Example 
int number = -782;
String numberAsString = Integer.toString(number);
The code is equivalent to: 
String numberAsString = "-782";
If you will try and search for solutions, this is one of
the most popular ways of converting an int to String. 
The
String class has several static methods that can convert most primitive types
to their String representation. This includes integers.
Example 
int number = -782;
String numberAsString = String.valueOf(number);
or 
String numberAsString = String.valueOf(-782);
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. 
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.
Example 
int number = -782;
Integer intInstance = new Integer(number);      
String numberAsString = intInstance.toString();
We can
shortened to: 
int number = -782;
String numberAsString = new Integer(number).toString();
or just: 
String numberAsString = new Integer(-782).toString();
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. 
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.
Syntax 
public static String format(String format, Object... args)
There are
many options on how to use this method. But for conversion purposes, here is a
simple example:
Example 
int number = -782;
String numberAsString = String.format ("%d", number);
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.
The class java.text.DecimalFormat is a
class that formats a number to a String representation following a certain
pattern. 
Example
Example
int number = 12345;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output 
12345
This is
an example that will convert to String but with commas 
int number = 12345;
DecimalFormat decimalFormat = new DecimalFormat("#,##0");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output 
12,345
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. 
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. 
StringBuffer Example 
int number = -782;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();
StringBuilder Example 
int number = -782;
StringBuilder sb = new StringBuilder();
sb.append(number);
String numberAsString = sb.toString();
Shortened Examples 
String numberAsString = new StringBuffer().append(-782).toString();
String numberAsString = new StringBuilder().append(-782).toString();
No comments:
Post a Comment