Java Integer toString() Method
Example
Convert a number to a string, and print the length of the string:
public class Main {
public static void main(String[] args) {
int myNum = 100;
String myString = Integer.toString(myNum);
System.out.println(myString);
System.out.println(myString.length());
}
}
Definition and Usage
The toString() method converts a number to a String (text).
You can call it on the class with a number, like Integer.toString(100), or on an Integer object, like myInt.toString().
Tip: With a second parameter, it writes the number in another number system. For example, Integer.toString(255, 16) returns "ff".
Syntax
One of the following:
public static String toString(int i)
public static String toString(int i, int radix)
public String toString()
The last version has no parameters. You call it on an Integer object, and it converts the value of that object.
Parameter Values
| Parameter | Description |
|---|---|
| i | Required in the first two versions. The number to convert. |
| radix | Optional. The base of the number system (the radix), for example 2 for binary or 16 for hexadecimal. The default is 10. |
Technical Details
| Returns: | A String with the number. |
|---|
Related Pages
❮ Integer Methods