Java is one of the most preferred programming languages. Software developers or engineers preparing for coding interviews should make it a point to review some of the basics of Java as a part of their prep. In this article, we’ll look at how to convert int to string in java. This process can come in handy while solving problems at tech interviews.
String in Java is a collection of characters, and there are many operations, such as finding substring and concatenation, that can be performed on a string but not on an integer. Say we want to concatenate two integers or want to find out if an integer contains a specific digit or not. We can simply convert integer to string in Java to do this type of operation easily.
There are many ways to convert integers to strings; let’s look at them one by one.
The return type of to String() is String, and it is present in many Java classes.
It can be used in two ways:
Here’s the code:
public class IK{
public static void main(String[] args){
Integer n = new Integer(100); // Passing int to constructor.
System.out.println(“Before conversion: ” + n);
// Object res to store the result.
String result = n.toString();
System.out.println(“After conversion: ” + result);
}
}
Output:
Before conversion: 100
After conversion: 100
This method is used to return the string representation of passed Integer (int or Integer).
Example: String.valueOf(Integer(100));
Here is how it’s implemented:
public class IK{
public static void main(String[] args){
Integer n = new Integer(100); // Passing int to constructor.
System.out.println(“Before conversion: ” + n);
// Object res to store the result.
String result = String.valueOf(n);
System.out.println(“After conversion: ” + result );
}
}
Output:
Before conversion: 100
After conversion: 100
StringBuilder class can be used to convert Integer to the string; this class builds a string by calling the append() method in it. We will create an object of StringBuilder and call method append() by passing out Integer. Then we can use the toString() method to get the answer in the String object.
The following is the code:
public class IK{
public static void main(String[] args){
Integer n = new Integer(100); // Passing int to constructor.
System.out.println(“Before conversion: ” + n);
// Creating StringBuilder object s
StringBuilder s = new StringBuilder();
// Passing Integer to append() method
s.append(n);
// Object res to store the result.
String res = s.toString();
System.out.println(“After conversion: ” + res);
}
}
Output:
Before conversion: 100
After conversion: 100
Syntax of this method is String.format(“%d”, int). Here, the first argument is any string containing %d.
%d stands for integer, which means the passed parameter is integer here. The second argument is the integer we want to convert. This method will return the String object containing a string representation of our number.
Here’s the code:
public class IK{
public static void main(String[] args){
Integer n = new Integer(100); // Passing int to constructor.
System.out.println(“Before conversion: ” + n);
// Object res to store the result.
String res = String.format(“%d”, n);
System.out.println(“After conversion: ” + res);
}
}
Output:
Before conversion: 100
After conversion: 100
In Java, we can simply concatenate an Integer to an empty string to convert int to string. Doing so will give a String representation of our Integer.
Here’s how:
public class IK{
public static void main(String[] args){
Integer n = new Integer(100); // Passing int to constructor.
System.out.println(“Before conversion: ” + n);
// Object res to store the result.
String res = “” + n;
System.out.println(“After conversion: ” + res);
}
}
Output:
Before conversion: 100
After conversion: 100
Here are a few sample interview questions related to converting an integer to string in Java:
If you’re looking for guidance and help to nail your next technical interview, sign up for our free webinar.
As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land their dream jobs at Google, Facebook, Apple, Netflix, Amazon, and other Tier-1 tech companies.
Join our webinar to learn more!
Use Integer.toString() or String.valueOf(int) for simple conversions.
Yes, you can convert int to string by appending the integer to StringBuilder and call toString() on it.
Using “” + int concatenates an integer with an empty string, creating a string.
You can use String.format(“%d”, int) to format and convert an integer to string in Java.
Integer.toString(int) is generally the fastest method for directly converting int to string in Java.
Recommended Reading:
Time Zone:
100% Free — No credit card needed.
Time Zone:
Land high-paying DE jobs by enrolling in the most comprehensive DE Interview Prep Course taught by FAANG+ engineers.
Ace the toughest backend interviews with this focused & structured Backend Interview Prep course taught by FAANG+ engineers.
Elevate your engineering career with this interview prep program designed for software engineers with less than 3 years of experience.
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
Time Zone:
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
25,000+ Professionals Trained
₹23 LPA Average Hike 60% Average Hike
600+ MAANG+ Instructors
Webinar Slot Blocked
Register for our webinar
Learn about hiring processes, interview strategies. Find the best course for you.
ⓘ Used to send reminder for webinar
Time Zone: Asia/Kolkata
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary