beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java int to String Conversion with examples

By Chaitanya Singh | Filed Under: Java Conversion

In this guide, we 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

Java – int to String using String.valueOf()

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

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);

Java – int to String using Integer.toString()

public class Example {
   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:

String is: 111
555111
666

Example: Converting int to String

This program demonstrates the use of both the above mentioned methods(String.valueOf() and Integer.toString()). Here we have two integer variables and we are converting one of them using String.valueOf(int i) method and other one using Integer.toString(int i) method.

public class IntToString {
    public static void main(String[] args) {
        
        /* Method 1: using valueOf() method
         * of String class.
         */
        int ivar = 111;
        String str = String.valueOf(ivar);
        System.out.println("String is: "+str);
        
        /* Method 2: using toString() method 
         * of Integer class
         */
        int ivar2 = 200;
        String str2 = Integer.toString(ivar2);
        System.out.println("String2 is: "+str2);
    }
}

Output:

String is: 111
String2 is: 200

3. String.format() method for conversion

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
❮ PreviousNext ❯

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 Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap