JAVAProgramming language

Java Convert int to String

You can transform Int into String in Java by using String.valueOf() and Integer.toString() methods. Alternately, we can employ the String.format() method and string concatenation operator, etc.

Scenario

It is typically used when it is necessary to display numbers in the text field since everything is presented as a string in format.

1) String.valueOf()

String.valueOf() method converts int to string. String.valueOf() procedure converts the int into String. Its valueOf() function is the static method for the String class. The method signature of the valueOf() method can be as follows:

public static String valueOf(int i)  

Java example of int conversion to string by using String.valueOf()

Let’s look at the easy method to convert int into String in Java.

int i=10;  
String s=String.valueOf(i);//Now it will return "10"  

Let’s look at the simple example of changing String to int in Java.

public class IntToStringExample1{  
public static void main(String args[]){  
int i=200;  
String s=String.valueOf(i);  
System.out.println(i+100);//300 because + is binary plus operator  
System.out.println(s+100);//200100 because + is string concatenation operator  
}}  

Output:

300
200100

2) Integer.toString()

It is the Integer.toString() procedure that converts the int into String. ToString() function is the method that remains static of the Integer class. The method signature of the toString() method can be shown below:

public static String toString(int i)  

Java Integer to String example with Integer.toString()

Let’s take a look at the easy code for converting int to String in Java by using the Integer.toString() Method.

int i=10;  
String s=Integer.toString(i);//Now it will return "10"  

Let’s take a look at the simplest example of how to convert String into an int in Java.

public class IntToStringExample2{  
public static void main(String args[]){  
int i=200;  
String s=Integer.toString(i);  
System.out.println(i+100);//300 because + is binary plus operator  
System.out.println(s+100);//200100 because + is string concatenation operator  
}}  

Output:

300
200100

3) String.format()

It is the String.format() procedure is used to format arguments in String. It was first introduced in JDK 1.5.

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

Java example of int converted to string by using String.format()

Let’s take a look at the simple method to convert int into String in Java using the String.format() Method.

public class IntToStringExample3{  
public static void main(String args[]){  
int i=200;  
String s=String.format("%d",i);  
System.out.println(s);  
}}  

Output:

200
About author

A self-motivated, highly dedicated, and ambitious learner. Experienced Technical Content Writer with a demonstrated history of working in the e-learning industry.
Related posts
JAVAProgramming language

Java Convert CHAR to INT

C++Programming language

C++ Recursion

CProgramming language

C Dereference Pointer

JAVAProgramming language

Java Convert Strings to Character(char)