-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertIntToString.java
More file actions
22 lines (20 loc) · 1.03 KB
/
convertIntToString.java
File metadata and controls
22 lines (20 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Question: Convert an integer input into String.
package strings;
import java.util.Scanner;
public class convertIntToString {
public static void main(String[] args) {
// Method 1: Using +Operator
// Add(+) the integer with an empty string("")
// [When we add an integer with a string using +Operator , it becomes string. Example: "Raj" (String) + 10 (int) = "Raj10" (String)]
Scanner sc=new Scanner(System.in);
System.out.print("Enter the integer: ");
int a=sc.nextInt();
System.out.println(a+"");
System.out.println(((Object)a+"").getClass().getSimpleName()); // This syntax is used to check the datatype of any variable
// Method 2: Using typecasting or inbuilt methods
System.out.print("Take anothor integer: ");
int b=sc.nextInt();
System.out.println(Integer.toString(b)); // String.valueOf(b) se bhi kiya ja sakta hai
System.out.println(((Object)Integer.toString(b)).getClass().getSimpleName());
}
}