How to Convert int Into String In Java

In this Tutorial We will see How to convert int into string in java . We can convert int into string in java using Integer.toString() , String.valueOf() , String.format() and StringBuffer or StringBuilder .

1. Convert int to String using Integer.toString()

In this example we will see how to convert int to string using Integer.toString() . toString() method is present in many java classes . It return a String .

Syntax :

public String toString() {
  }

This method returns a String object representing this Integer’s value.First We will create Integer object from int , then we will  convert integer to String .

public class IntToStringUsingToString {
	
	public static void main(String[] args) {
		int num =100;
		Integer integer = new Integer(num);
		String str = integer.toString();
		System.out.println("String is ="+ str);
	}

}

Output :

String is =100

2. Convert int to String using String.valueOf()

We can convert int to string using String.valueOf() method returns the string representation of the int argument.

Syntax :

public static String valueOf(int i) {
    }

Parameters: i an int.
Returns: a string representation of the int argument.

public class IntToStringUsingValueOf {
	public static void main(String[] args) {
		int num = 100;
		String str = String.valueOf(num);
		System.out.println("string is ="+ str);
	}
}

Output :

string is =100

3. Convert int to String using String.format()

We can convert int to String using String.format() method with “%d”

public class IntToStringUsingFormat {
      public static void main(String[] args) {
		  int num =100;
		  String str =String.format("%d",num);  
          System.out.println("String is ="+ str);
	}
}

Output :

String is =100

4. Convert int to String using StringBuffer or StringBuilder

StringBuilder or StringBuffer can convert int to String. For this first we will create StringBuilder or StringBuffer instance  , then append int value to it. We can get String from StringBuilder or StringBuffer calling toString() method.

public class IntToStringUsingStringBuilder {
	public static void main(String[] args) {
		int num =100;
		StringBuilder builder = new StringBuilder();
		builder.append(num);
		String str = builder.toString();
		System.out.println("String is ="+ str);
	}
}

Output :

String is =100

In this tutorial we have discussed how to convert int to String using different methods. If you are looking more String interview Question , then these question list id for you.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 2 =