Java Program to Convert int to String
What is int in Java ?
“int” is a primitive data type in Java that is used to represent integers (whole numbers) within a certain range of values. The range of values that can be stored by an “int” is from -2^31 to 2^31-1. The “int” data type is commonly used in mathematical operations and to represent arrays of integers.
What is String in Java ?
In Java, the “String” data type is a class that represents a sequence of characters. It is used to store and manipulate text-based data. Unlike primitive data types, such as “int” or “float”, which hold a single value, a String object can hold multiple characters.
Ways to Convert int to String :
Syntax :
int myInt = 123; String myString = String.valueOf(myInt);
Syntax :
int myInt = 123; String myString = Integer.toString(myInt);
Syntax :
int myInt = 123; String myString = "" + myInt;
Example 1 :
public class Main { public static void main(String[] args) { int myInt = 123; String myString = String.valueOf(myInt); System.out.println("My int value as a String is: " + myString); } }
Output :
My int value as a String is: 123
Example 2 :
public class Main { public static void main(String[] args) { int myInt = 123; String myString = Integer.toString(myInt); System.out.println("My int value as a String is: " + myString); } }
Output :
My int value as a String is: 123
Example 3 :
public class Main { public static void main(String[] args) { int myInt = 123; String myString = "" + myInt; System.out.println("My int value as a String is: " + myString); } }
Output :
My int value as a String is: 123
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment