CodeGym/Java Blog/Strings in Java/How to convert int to String in Java
Author
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

How to convert int to String in Java

Published in the Strings in Java group
members
In this article we are going to discuss converting int (primitive type) and Object type (wrapper) Integer to String. There are many ways to do it in Java.

How to convert int to string in Java

  1. Convert by adding an empty string.

    The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you’ll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5, just define x + "" and you’ll get your new String.

    Here is an example:

    //  converting int to string, Java
    public class Demo {
       public static void main(String[] args) {
    
           int x = 5;
           //  java int to string
           String xText = x + "";
           //  the result output
           System.out.println("convert int to String, Java: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("adding int = 5 and String = \"5\". The result is a new String = " + xText + x);
           //  integer to string, Java code
           Integer y = 7;
           String yText = y + "";
           System.out.println("convert Integer to String: " + yText);
           System.out.println("our Integer: " + y);
           System.out.println("adding Integer = 7 and String = \"7\". The result is a new String = " + y + yText);
       }
    }

    The output is:

    
    convert int to String, Java: 5
    our int: 5
    adding int = 5 and String = "5". The result is a new String = 55
    convert Integer to String: 7
    our Integer: 7
    adding Integer = 7 and String = "7". The result is a new String = 77
    

  2. Java convert int to string using Integer.toString(int)

    Object class is a root class in Java. That means every Java class is directly or indirectly inherited from the Object class and all Object class methods are available for all Java classes.

    Object has a special method toString() to represent any object as a string. So, every Java class inherits this method as well. However the good idea is to override this method in your own classes to have an appropriate result.

    The Integer class’ toString() method returns a String object representing the specified int or Integer parameter.

    Its Syntax:

    public static String toString(int i)

    The method converts argument i and returns it as a string instance. If the number is negative, the sign will be kept.

    Example:

    //  java integer to string using toString method
    
    public class Demo {
       public static void main(String[] args) {
    
           int x = -5;
           //  java convert int to string using Integer.toString
           String xText = Integer.toString(x);
           //  the result output
           System.out.println("convert int to String: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + xText + Integer.toString(x));
    
    
       }
    }

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    You may use the toString method to convert an Integer (wrapper type) as well.

    Integer number = -7;
    String numberAsString = Integer.toString(number);
    System.out.println("convert Integer to String: " + numberAsString);

    The result is:

    convert Integer to String: -7

    You may use special Integer.toString method toString(int i, int base) that returns a string representation of the number i with the base base and than to String. For example

    The syntax is:

    public static String toString(int i, int base)

    Here is an example:

    int a = 255;
    //  binary
    String customString = Integer.toString(a, 2);
    System.out.println(customString);

    The output is a String binary representation of decimal number 255:

    
    11111111
    

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

    Method String.valueOf(int) returns the string representation of the int argument.

    The syntax of the method is:

    public static String valueOf(int i)

    Here is an example of Java convert int to String using String.valueOf(int):

    public class Demo {
       public static void main(String[] args) {
           int z = -5;
           //  Java int to String converting
     String zText = String.valueOf(z);
           //  the result output
           System.out.println("convert int to String: " + zText);
           //  the int output
           System.out.println("our int: " + z);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + zText + z);
       }
    }

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    You may do the same with an Integer (wrapper type of int):

    Integer number = -7;
    String numberAsString = String.valueOf(number);
    System.out.println("convert Integer to String: " + numberAsString);

    The output will be:

    convert Integer to String: -7

  4. Convert using DecimalFormat

    java.text.DecimalFormat is a class defined in java.text package and a subclass of NumberFormat. It is used for formatting a decimal number to a string representing following a certain pattern. We can use it for Integers as well.

    Example:

    import java.text.DecimalFormat;
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 31415;
           DecimalFormat decimalFormat = new DecimalFormat("#");
           String myNumberAsString = decimalFormat.format(myNumber);
           System.out.println(myNumberAsString);
       }
    }

    The output is:

    
    31415
    

  5. Convert using String.format()

    String.format() is one more way to convert an Integer to String Object.

    Syntax

    public static String format(String format, Object... args)

    Example

    public class Demo {
       public static void main(String[] args) {
           int myNumber = 35;
           String myNumberAsString = String.format("%d", myNumber);  //  %d converter defines a single decimal integer variable.
           System.out.println(myNumberAsString);
       }
    }

    The output is:

    35
    

Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Ashish RajAnand
Level 13 , Bhilai , India
22 March 2020, 03:28
nice
Anthony Chalk
Level 30 , London, United Kingdom
22 November 2019, 13:11
Great lesson. Does anyone have links to any articles that more fully explain the differences and advantages/disadvantages between all these methods?
Maryem Vickers
Level 7 , HT..., United Kingdom
3 June 2020, 18:33
Maybe I could see...
Maryem Vickers
Level 7 , HT..., United Kingdom
3 June 2020, 18:35
Here: https://www.geeksforgeeks.org/different-ways-for-integer-to-string-conversions-in-java/ I hope this is helpful.
Maryem Vickers
Level 7 , HT..., United Kingdom
3 June 2020, 18:36
Although it is not a Code Gym arcticle...
Renat Mukhametshin
Level 16 , Pervouralsk, Russain Federation
7 August 2019, 21:33
good
Robson Cassiano
Level 9 , Campinas, Brazil
26 July 2019, 13:49
/* Comment has been deleted */
CodeGym
Level 41
Expert
26 July 2019, 16:20
Thanks)