Java Program to Convert int to String - The Coding Shala

Home >> Java Programs >> Convert int to String

 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:

  1. By using String.valueOf() method.
  2. By using Integer.toString() method.
  3. 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
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Single Number 3 LeetCode Solution - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala