In the previous article we have discussed Java Program to Convert String to Boolean
In this article we will see how to convert a character to string.
Program to Convert int to String
Before starting the actual program, let’s see some examples of both the types.
Example-1: int type int a = 4; int b = 78;
Example-2: String type String a = "BtechGeeks"; String b = "B";
Let’s see different ways to do.
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Method-1 : Java Program to Convert int to String Using valueOf() method
Integer can be converted to string by using valueOf() , let’s see how it actually works.
String.valueOf() is a method which will simply typecasts below-given parameter to strings always. It is an inbuilt method of String class in java.
Approach :
- Take a Integer value and store it in a Integer variable
input1. - Then pass that
input1variable as parameter toString.valueOf( )method which will convert the Integer to string value and return it . - Store that string value in a variable
output. - Display the result .
Program :
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// creating scanner object
Scanner sc = new Scanner(System.in);
// input a integer value through scanner class
System.out.print("Enter a Integer number : ");
int input1=sc.nextInt();
// converting to string
String output = String.valueOf(input1);
System.out.println("Converted String is : "+output);
}
}
Output : Enter a Integer number : 5 Converted String is : 5
Method-2 : Java Program to Convert int to String Using toString() method
Integer can be converted to string by using toString() , let’s see how it actually works.
Approach :
- Take a Integer value and store it in a Integer variable
input1. - Then pass that
input1variable as parameter toInteger.toString()method which will convert the Integer to string value and return it . - Store that string value in a variable
output. - Display the result.
Program :
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// creating scanner object
Scanner sc = new Scanner(System.in);
// input an integer value through scanner class
System.out.print("Enter a Integer number : ");
int input1=sc.nextInt();
// converting to string
String output = Integer.toString(input1);
System.out.println("Converted String is :"+output);
}
}
Output : Enter a Integer number : 5 Converted String is :5
Method-3 : Java Program to Convert int to String Using “+” operator
Integer can be converted to string by using “+” operator.Let’s see how it works.
Approach :
- Take a Integer value and store it in a Integer variable
input1. - Take a string variable and concatenate “
+” with the input variable which will treated as String. - Display the result.
Program :
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// creating scanner object
Scanner sc = new Scanner(System.in);
// input an integer value through scanner class
System.out.print("Enter an Integer number : ");
int input1=sc.nextInt();
// converting to string
String output = " " + input1;
System.out.println("Converted String is :"+output);
}
}
Output : Enter a Integer number : 5 Converted String is :5
Method 4 : Java Program to Convert int to String Using format() method
Integer can be converted to string by using format() method. Let’s see how it actually works.
Approach :
- Take a Integer value and store it in a Integer variable
input1. - Then pass that
input1variable as parameter toString.format()method which will convert the Integer to string value and return it . - Store that string value in a variable
output. - Display the result.
Program :
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// creating scanner object
Scanner sc = new Scanner(System.in);
// input an integer value through scanner class
System.out.print("Enter an integer number : ");
int input1=sc.nextInt();
// converting to string
String output =String.format("%d", input1);
System.out.println("Converted String is :"+output);
}
}
Output: Enter a Integer number : 5 Converted String is :5
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Related Java Program: