How to Convert Integers to Strings in Java

java integer to stringJava is a high-level programming language. It’s a partial object oriented programming language that follows concepts of classes and objects, polymorphism and encapsulation. It consists of many classes and packages that extend the scope of its application. Java contains several data types to store values such as an integer, float, short, character and string. However, “String” in Java is not a data type. It is a class that has string objects.

There are two classes of Strings in Java. The String class is immutable and cannot be changed. StringBuffer class is mutable class and can be appended.

Learn Java programming from scratch through Udemy.com

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

Declaration of Data Types

int abc=10;
double bcd=12.23;
//String declaration
String abc=“”Hello”;
StringBuffer str1=new StringBuffer(”Hello World”);

Numeric values are used for performing calculation and manipulations. Every data type in Java has a size limit and value range.

Want to know about core Java concepts? Take a tutorial course at Udemy.com

For instance:

int x=1000000000000000;

The above code throws an error that the integer is too large, tut the same value can be stored in a string object.

String str=“1000000000000000”;

The above code will store the numeric value as a String without the error.

There are three ways to convert an integer to a string:

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

Using valueOf(): This method is present in every numeric subclass of Java primitive data types such as a float, integer, double, short and byte. It is used for String conversions of your data. It is a static method that does not need instantiation before being called.

The following code is an example of the “valueOf() syntax:

public static String valueOf(int i)

The following code is the String to integer conversion

int a=Integer.valueof(“123.2”);

Conversely, the following code is the conversion from integer to String:

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

The integer variable is passed in the valueOf() function and converts the integer to a string named “str.”

Another full development example:

class StringDemo {
public static void main(String[] args) {
  double d = 756.226772;
//Double to String  conversion
   String str = String.valueOf(d);
System.out.println("This is Double to String conversion " + str);
int i_am_int = 756;
//Integer to String conversion
   String str1 = String.valueOf(i_am_int);
    System.out.println("This is the conversion from Integer to String " + str1);
   }
}

Output:

This is Double to String conversion756.226772

This is the conversion from Integer to String 756

 

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 the console output. It works similarly for double data types.

Integer.toString(): This is a static method used for conversion from integer to a String and has two different constructors based on the number of 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, but by default it is set to “10” for the 10-based decimal system. Both the functions return a String as a result of the integer input. If the radix value is passed, then the String returned is defined by the radix value.

For example:

class StringDemo {
public static void main(String[] args) {
double d = 756.48;
//Double to String convert
String str = Double.toString(d);
System.out.println("This is Double to String conversion " + str);
int i_am_int = 756;
// Integer to String convert
String str1 = Integer.toString(i_am_int);
System.out.println("This is the conversion from Integer to String " + str1);
   }
}

Output:

This is Double to String conversion 756.48

This is the conversion from Integer to String 756

In the above program, the value from the variable “d” is taken and passed in the Integer.toString() function. The string returned is printed on the screen as console output.

Get introduced to Java programming through a tutorial on Udemy.com

This process 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.  The “format” parameter includes a format specifier or a fixed-text format. The ”args” parameter 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.

 

For example:

class StringDemo {
public static void main(String[] args) {
int i_am_in = 756;
//User Integer Input
Scanner sc= new Scanner(System.in);
System.out.println("Enter Integer to Convert");
int i_am_int=sc.nextInt();
       String str=""+ i_am_int;
       String str2 = String.format("%d", i_am_int);
System.out.println("Integer entered by user converted to String format method" + str2);
System.out.println("Integer converted to String format method" + str2);
   }
}

 

Enter Integer to Convert:

756

Output

Integer entered by user converted to String format method 756

Integer converted to String format method 756

 

In the above program, the input from the user is taken as an integer form and passed to the format function. The format function converts the integer value to String form and returns it to the “str2” variable. 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.

To “catch” errors and perform error checking for invalid characters, the following code can be used as an example:

Try {
           // use format() method
String string6 = String.format("%d", mainInt);
System.out.println("With format method: string6 = " + string6);
       }
catch(IllegalFormatException e1)
{
System.err.println("IllegalFormatException: "+ e1.getMessage());
       }
catch(NullPointerException e2)
{
System.err.println("NullPointerException: "+ e2.getMessage());
 }

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

For Example:

class StringDemo {
public static void main(String[] args) {
int i_am_in = 756;
String str=”” + i_am_int.toString();
System.out.println("Integer converted to String format method" + str);
   }
}

Enter Integer to Convert:

756

Output

Integer entered by user converted to String format method 756

 

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.

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