Convert int to String in Java
Tags: String int Integer Convert DecimalFormat
In this Java core tutorial, we learn how to convert int value into String value in Java via different solutions.
Using String.valueOf() method to instantiate String from a int value
ConvertIntToStringExample1.java
public class ConvertIntToStringExample1 {
public static void main(String[] args) {
int value1 = 987;
String value2 = String.valueOf(value1);
System.out.println("int value: " + value1);
System.out.println("String value: " + value2);
}
}
int value: 987
String value: 987
Using Integer.toString() static method to convert an int value to String
ConvertIntToStringExample2.java
public class ConvertIntToStringExample2 {
public static void main(String[] args) {
int value1 = 456;
String value2 = Integer.toString(value1);
System.out.println("int value: " + value1);
System.out.println("String value: " + value2);
}
}
int value: 456
String value: 456
Using Integer.toString() to return a String from an Integer object
ConvertIntToStringExample3.java
public class ConvertIntToStringExample3 {
public static void main(String[] args) {
Integer value1 = 123;
String value2 = value1.toString();
System.out.println("int value: " + value1);
System.out.println("String value: " + value2);
}
}
int value: 123
String value: 123
Using String.format() to format an int value to String
ConvertIntToStringExample4.java
public class ConvertIntToStringExample4 {
public static void main(String[] args) {
Integer value1 = 67;
String value2 = String.format("%d", value1);
System.out.println("int value: " + value1);
System.out.println("String value: " + value2);
}
}
int value: 67
String value: 67
Using DecimalFormat class to format int value to String
ConvertIntToStringExample5.java
import java.text.DecimalFormat;
public class ConvertIntToStringExample5 {
public static void main(String[] args) {
int value1 = 980;
DecimalFormat decimalFormat = new DecimalFormat("#");
String value2 = decimalFormat.format(value1);
System.out.println("int value: " + value1);
System.out.println("String value: " + value2);
}
}
int value: 980
String value: 980
Happy Coding 😊