BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java int to String Conversion With Examples

Last Updated: November 15, 2022 by Chaitanya Singh | Filed Under: java

In this guide, you will learn how to convert an int to string in Java. We can convert int to String using String.valueOf() or Integer.toString() method. We can also use String.format() method for the conversion.

1. Convert int to String using String.valueOf()

String.valueOf(int i) method takes integer value as an argument and returns a string representing the int argument.

Method signature:

public static String valueOf(int i)

Parameters:
i – integer that needs to be converted to a string

Returns:
A string representing the integer argument

Let’s see the java program:

public class JavaExample {
  public static void main(String args[]) {
    int ivar = 111;
    String str = String.valueOf(ivar);
    System.out.println("String is: "+str);
    //output is: 555111 because the str is a string
    //and the + would concatenate the 555 and str
    System.out.println(555+str);
  }
}

Output:

Java Int to String conversion Output

2. Convert int to String using Integer.toString()

Integer.toString(int i) method works same as String.valueOf(int i) method. It belongs to the Integer class and converts the specified integer value to String. for e.g. if passed value is 101 then the returned string value would be “101”.

Method signature:
public static String toString(int i)

Parameters:
i – integer that requires conversion

Returns:
String representing the integer i.

Example:

int ivar2 = 200;
String str2 = Integer.toString(ivar2);

Let’s see the complete java program:

public class JavaExample {
  public static void main(String args[]) {
    int ivar = 111;
    String str = Integer.toString(ivar);
    System.out.println("String is: "+str);
    //output is: 555111 because the str is a string
    //and the + would concatenate the 555 and str
    System.out.println(555+str);

    //output is: 666 because ivar is int value and the
    //+ would perform the addition of 555 and ivar
    System.out.println(555+ivar);
  }
}

Output:

Convert int to string using Integer.toString() Output

3. Convert int to String using String.format() method

public class JavaExample{
  public static void main(String args[]){
    int num = 99;
    String str = String.format("%d",num);
    System.out.println("hello"+str);
  }
}

Output:

hello99

We can also use format() method to add leading zeroes to the String. Let’s take an example where we are convert int to String with leading zeroes.

Here, %05d means:

  • 0 means pad the String with leading zeroes.
  • 5 means set the width of the output string to 5.
public class JavaExample{
  public static void main(String args[]){
    int num = 15;

    //leading zeroes to make the string length to 5
    String str = String.format("%05d",num);

    //leading zeroes to make the string length to 3
    String str2 = String.format("%03d",num);
    System.out.println(str);
    System.out.println(str2);
  }
}

Output:

00015
015

Recommended Posts

  • Java String to int Conversion
  • Java int to float Conversion
  • Java int to long Conversion
  • Java int to double Conversion
  • Java int to char Conversion

Top Related Articles:

  1. Java – String regionMatches() Method example
  2. Java – float to String conversion
  3. Java Integer byteValue() Method
  4. Java String join() method
  5. Java String contains() method

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. steve says

    April 9, 2016 at 1:37 AM

    This is very well presented instruction. One possible addition would be adding explanation of why one might want to do (the example). IE why would I want to convert a integer to a string? Probably a simple reason but none come to mind besides I could do it.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap