Convert int to String in Java


JavaViews 3940

In this tutorial, we will learn and understand the different ways to convert int to String in Java. We will also demonstrate Java Integer to String conversion with detailed examples.

Convert int to String in Java

Different methods of Integer to String conversion

Java Integer to String conversion

Convert int to String using String.valueOf()

The String class contains a static method valueOf() that converts an integer to a String value. We mainly use this when we want to display a number in the text field that contains only Strings.

public static String valueOf(int i);

As you can see in the above syntax, the valueOf() method accepts an integer value as a parameter and performs the conversion, and returns a String value.

Let us see a simple example of Java Integer to String conversion using the valueOf() method. This code converts the value of an integer variable into a string and returns “100”. Hence we can concatenate any other string value to this variable.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    String s = String.valueOf(a);
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

Convert int to String using toString() method

The toString() method is a static method that is part of the Integer class which converts an Integer to a String value.

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

As you can see from the above syntax, the toString() method converts the integer value that we pass as an argument to a String and returns the converted string value. We can also specify the radix parameter that denotes the base conversion. By default, it is 10 that represents the decimal base format.

Below is an example that demonstrates the conversion of integer to String using the toString() method in Java.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    String s = Integer.toString(a);
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

This example shows converting an int to a string with a specific radix base value. Here, since we mention the radix value as 8, it represents a string value of the integer with base 8(octal) representation.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    String s = Integer.toString(a, 8);
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 144
144 years

Using String.format() method

The format() method of the String class is available from JDK 1.5 that helps to convert an integer to a string value in Java based on the specified format.

public static String format(String format, Object.. args);

The first parameter represents the string format which in this case should be “%d”. The second parameter is the integer value that we want to convert. So when we use the format() method, it replaces “%d” with the integer value and converts it into a String value.

This example shows you how to convert an int to String in Java using the format() method.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    String s = String.format("%d", a);
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

Using DecimalFormat

Another way to convert an Integer to String in Java is to use the DecimalFormat class that is present in the java.text.DecimalText.

import java.text.DecimalFormat;

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    DecimalFormat df = new DecimalFormat("#");
    String s = df.format(a);

    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

We can also specify the format of the string by using the second parameter. This will insert a comma based on the parameter passed. In this example, it inserts a comma after every 3 digits.

import java.text.DecimalFormat;

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 10000000;
    DecimalFormat df = new DecimalFormat("#, ###");
    String s = df.format(a);
    
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 10,000,000
10,000,000 years

Using concatenation with an empty string

Another simple way to convert an Integer to a String is to concatenate with an empty string. Let us look at the below example. When we concatenate an integer value with an empty string, it results in a string value.

Convert Int to String in Java

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    String s = "" + a;
    
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

Using StringBuilder and StringBuffer

We can also use the toString() method of the StringBuffer class to convert an int to String. Below is an example that uses StringBuilder for Integer to String conversion. First, we need to append the integer variable to the StringBuffer instance using the append() method and then convert it to a string using the toString() method.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    StringBuffer sbuf = new StringBuffer();
    sbuf.append(a);
    String s = sbuf.toString();
    
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

Below is an example of using the StringBuilder for Integer to String conversion in Java. Both StringBuilder and StringBuffer have the same functionality but StringBuilder is not thread-safe.

public class ConvertIntToString {

  public static void main(String[] args) {
    int a = 100;
    StringBuilder sb = new StringBuilder();
    sb.append(a);
    String s = sb.toString();
    
    System.out.println("String value: " + s);
    System.out.println(s + " years");

  }

}
String value: 100
100 years

Reference

Translate ยป