Integer to String Conversion in Java.

Conversion of Data types from one data type to another is important and is a basic part of learning any programming language and programming concepts. These conversions serve as a building blocks for larger programs.
Java consist of many data types to store values of different forms like integer, float, short, character and string. We already know that String in Java is not a data type,they are a class that has string objects.

For performing calculations integer, double and float values are used in Java. Often these numeric values have to be used as a part of set of characters and thus, a need to convert them into strings arises.

For instance for Integer to String Conversion in Java

int x=1000000000000000; will give error integer too large.
But the same value can be easily stored in a string object .
String str=“1000000000000000”;

There are three way to convert integer to string:

Integer.toString()
String.valueOf()
String.format()

Using valueOf(): This method is present in every numeric subclass of primitive data type like float, integer, double, short and byte. It is used for the conversion of the data within different data types and to and from strings. It is a static method and does not need any class object to get called.

Syntax: public static String valueOf(int i)

Conversion from Integer to String

int abc=25;
String str=String.valueOf(abc);

The integer variable is passed in the valueOf() function and converts the integer in to string.

In the above program the value from the variable “d” is taken and passed in the String.valueOf() function and the string returned is printed on the screen as output. It works similarly for double data types.

Integer.toString(): This is a static method used for conversion from integer to string and has two different constructors based on the parameters.

public static String toString(int i)
public static String toString(int i, int radix)

The “int i” parameter in both functions is the value passed when the method is called for the conversion from integer to string.

The “int radix” is a user based number system used to denote the string in string objects. Radix is an optional value and by default is taken as “ten” for the decimal system. Both the functions return a String as a result of integer input. If radix value is given then the string returned is defined by the radix.

In the above program the value from the variable “d” is taken and passed in the Integer.toString() function and the string returned is printed on the screen as output.
It works similarly for double data type

Using String.format(): Another way to convert integer to string is format method().This method also has two constructs.

Syntax:

public static String format(Local i, String format, Object.. args)
public static String format(String format, Object.. args)

The “Local i ”  is addressed during  the string formatting. “format” this parameter includes a format specifier or a fixed text sometimes. ”args” is the format specifier passed in the “format” argument. The value returned is the formatted string as specified by the parameters. This method was introduced in JDK1.5.

In the above program the input from the user is taken in an integer form and passed to the format function. The format function converts the integer value in the string form and returns it. If the function is unable to parse the input integer value then the compiler throws “IllegalFormatException”. Also, the exception is thrown when the null value is entered or if an empty string is given as input.

We also have a fourth method to convert the numeric or integer value into string by using “+” operator also called the concatenation operator.

The above example would simply add the integer value in the string. The plus operator is overloaded. The concatenation is generally not preferred for conversion to string.

Conclusion

Any method shown above can be used for numeric to string conversions.

For String to Integer conversion follow this link.

Leave a Reply

Your email address will not be published. Required fields are marked *