How to Convert int to String in Java

There is a different ways to convert int to string in java. Java allows you to convert values ​​from one datatype to another datatype. You can convert a string variable to numeric value and numeric value to string variable. In this article, we will see how to convert int to string in java.

Below are the different ways to convert int to string in Java:

  1. Integer.toString()
  2. String.valueOf()
  3. String.format()
  4. StringBuffer and StringBuilder

How to Convert int to String in Java?

1. Integer.toString()

The Integer.toString() method convert int to string. The toString() is static method of Integer class. The syntax of toString() method is given below:

public static String toString(int i)

Example:

how to convert int to string in java
Integer.toString()

2. String.valueOf()

The String.valueOf() method converts int to String. The valueOf() is the static method of String class. The syntax of valueOf() method is given below:

public static String valueOf(int i)

Example:

How to Convert int to String in Java
String.valueOf()

3. String.format()

The String.format() method is used to format given arguments into String. It is introduced since Jdk 1.5. The syntax of String.format() method is given below:

public static String format(String format, Object… args) 

Example:

How to Convert int to String in Java
String.format()

4. StringBuffer and StringBuilder

StringBuilder and StringBuffer classes are used to combine multiple values ​​into a single string. StringBuffer is thread safe but slow, while StringBuilder is not thread safe but fast. Below I have provided an example.

Example:

How to Convert int to String in Java
StringBuffer and StringBuilder

Other Popular Articles:

What is String in Java

How to Reverse a String in Java

Leave a Comment