How to convert int to string in Java
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 definex + ""
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
- 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: -7You 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 exampleThe 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
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: -7Convert using DecimalFormat
java.text.DecimalFormat
is a class defined injava.text
package and a subclass ofNumberFormat
. 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
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
GO TO FULL VERSION