How to convert an Integer to a String in java?

There are the following ways to convert an integer into a String in java-

Using valueOf() method -

The valueOf() method takes an integer value as an argument and returns a string representing the int argument. This is the most preferred way of converting int variable to string in Java.

We can check the type of a variable in Java by calling getClass().getSimpleName() method -

 public static void main(String[] args) {

    // create int variable
    int num = 14;

    // convert int to string using valueOf()
    String str = String.valueOf(num1);

    // print the integers as string variables
    System.out.println(str + "whose data type is-");    
    System.out.println(str.getClass().getSimpleName());
  }

Output-

14 whose data type is-
String

The toString() method -

The toString() belongs to the Integer class and converts the specified integer value to String. For example, if the value is 123 then the returned string value would be "123".

The syntax is-

public static String toString(int i)

Where the parameter 'i' is the integer that requires conversion and the method toString() returns a String representing the integer i. The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved. Example-

We can check the type of a variable in Java by calling getClass().getSimpleName() method-

public static void main(String args[])
  {
    int s1 = 123;
    //here, Integer is the wrapper class
    String str1 = Integer.toString(s1);
    System.out.println("String str1 is : " + str1);
    System.out.print("type:  ");
    System.out.println(str1.getClass().getSimpleName());
  }

Output-

String str1 is: 123
 type:  String
Difference between valueOf() and toString() method -

The valueOf() method will transform a given object that is null to the String "null", whereas toString() will throw a NullPointerException.

Also, String.valueOf() can be used for primitive data types but toString() cannot be used on primitive data types. The below example will make it more clear to you-

public static void main(String args[]) {  
    String str = null;
    // This will print the String as "null"    
    System.out.println(String.valueOf(str));  

    // This will throw a NullPointerException    
    System.out.println(str.toString()); 
} 

Output-

null
Exception in thread "main" java.lang.NullPointerException

Using + operator -

Here, we will use the string concatenation operation to convert an integer into the string. Concatenation is not meant for conversion, but you can concatenate your integer to an empty string because when you add an integer to a string, the result is a string. For example-

We can check the type of a variable in Java by calling getClass().getSimpleName() method -

 public static void main(String[] args) {
    // create int variables
    int num1 = 123;

    // convert int to string using + sign (concat)
    String str1 = "" + num1;

    // print the integer values as string variables
    System.out.println(str1 + " is now converted to type:");  
    System.out.println(str1.getClass().getSimpleName());
  }

Output-

123 is of type:
String

Using String.format() method -

The String.format() method returns a string formatted according to the format specifier and the specified arguments. If you pass an incompatible type eg, if you pass Integer for specifier '%s' (which only accepts String object then the format() method will throw an IllegalFormatConversionException.

Some common specifiers are '%s' for String, '%d' for integer, and '%f' for floating-point number. Here we will see only the "%d" specifier since it converts the int type to String.

int i = 123;
//using String.format() method to convert int i to string str
String str = String.format("%d", i);
System.out.println(str);

Output-

123

Using StringBuffer and StringBuilder -

The StringBuffer and StringBuilder are classes used to concatenate multiple values into a single String. They use the append() method. We create an object of one of these two classes and call this method by passing our integer.

public static void main(String[] args) {
Integer i = new Integer(123);
// StringBuilder or StringBuffer can be used
StringBuilder st = new StringBuilder();
st.append(i);
System.out.println(st);
System.out.println("hello" + st);
System.out.println("since hello has been concatenated with 'st'
 hence int i has been converted into string");
  }

Output-

123
hello123
since hello has been concatenated with 'st' hence int i has been converted into string
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions