Java Program to Convert int to String - The Coding Shala
In this post, we will learn how to convert int to string in Java.
Java Program to Convert int to String
There are a few ways by using those we can convert int data type to String data type in Java. The most common ways are below:
- By using String.valueOf() method.
- By using Integer.toString() method.
- By using String.format() method.
Now let's see these methods one by one with examples in Java.
Java Program:
/*** https://www.thecodingshala.com/ */ public class Main { public static void main(String[] args) { int num1 = 100; // using String.valueOf(int) String str1 = String.valueOf(num1); System.out.println("Converted String str1 is: " + str1); System.out.println("5 + int number is: " + (5 + num1)); System.out.println("5 + str1 is: " + (5 + str1)); // using Integer.toString(int) String str2 = Integer.toString(num1); System.out.println("Converted String str2 is: " + str2); System.out.println("10 + str2 is: " + (10 + str2)); // using String.format() String str3 = String.format("%d", num1); System.out.println("Converted String str3 is: " + str3); System.out.println("1 + str3 is: " + (1 + str3)); } }
Output:
Converted String str1 is: 100
5 + int number is: 105
5 + str1 is: 5100
Converted String str2 is: 100
10 + str2 is: 10100
Converted String str3 is: 100
1 + str3 is: 1100
Other Posts You May Like
- Java Program to Convert int to long
- Java Program to Convert long to int
- Java Program to Convert int to char
- Java Program to Convert char to int
- Java Program to Take Input From User
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.
Comments
Post a Comment