Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

How to Convert an Integer to a String in Java

Last updated by Abhinav Rawat on Apr 01, 2024 at 01:48 PM | Reading time: 5 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

How to Convert an Integer to a String in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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 an integer can be converted into a string in Java. This process can come in handy while solving problems at tech interviews.

Here’s what we’ll cover:

  1. Introduction
  2. 5 Different methods to convert integer to string in Java 
  3. Java interview questions on converting an integer to a string

Introduction

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 integers to strings to do this type of operation easily. 

Different Methods to Convert an Integer to String in Java

There are many ways to convert integers to strings; let's look at them one by one. 

Method 1. Using toString() Method

The return type of to String() is String, and it is present in many Java classes.  

It can be used in two ways:

  1. By the static function of the Integer class like Integer.toString(100)
  2. By using the regular version of the Integer class’s object. We need first to convert int to Integer because this method cannot be used with the primitive type int. We can use a simple assignment using the = operator. 

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

Method 2: Using String.valueOf()

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

Method 3: Using StringBuilder

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

Method 4 (Indirect Way): Using String.format()

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

Method 5 (Indirect Way): Using Concatenation With Empty String

In Java, we can simply concatenate an Integer to an empty string, and it 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

Interview Questions on Converting Integer to String in Java

Here are a few sample interview questions related to converting an integer to a string in Java: 

  1. How to convert string to int in Java?
  2. How to convert int to string in Java effectively?
  3. Why is string immutable in Java?
  4. How to convert char to integer in Java?
  5. How to convert integer to char in Java?
  6. How to convert char array to string in Java?
  7. How to convert double to string in Java?

Recommended Reading

Check out our learn page for more articles on Java:

Ace Your Next Tech Interview With Interview Kickstart!

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!

---------

Article contributed Omkar Deshmukh

Last updated on: 
April 1, 2024
Author

Abhinav Rawat

Product Manager @ Interview Kickstart | Ex-upGrad | BITS Pilani. Working with hiring managers from top companies like Meta, Apple, Google, Amazon etc to build structured interview process BootCamps across domains

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

How to Convert an Integer to a String in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image