The toString() method of Java Integer class returns a String object which represents this Integer’s value.
The second syntax returns a String object which represents the specified integer.
The third syntax returns a string representation of the specified argument in the radix specified by the given radix.
Syntax
- public String toString ()
- public static String toString (int i)
- public static String toString (int i, int radix)
Parameters
The parameter ‘i’ represents an integer to be converted to a string and the parameter ‘radix’ represents the radix to use in the string representation.
Return Value
This method returns a String which represents the same value as this argument in base 10 or in the specified radix.
Example 1
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerToStringExample1 { public static void main(String[] args) { Integer val=5667; String val1=Integer.toString(val); //It returns a string representation of the value of this object in base 10. System.out.println("String Value : "+val1); } } |
Output
1 2 3 |
String Value : 5667 |
Example 2
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerToStringExample2 { public static void main(String[] args) { Integer val=Integer.MAX_VALUE; String val1=val.toString(); //It returns a string representation of the value of this object in base 10. System.out.println("String Value : "+val1); } } |
Output
1 2 3 |
String Value : 2147483647 |
Example 3
1 2 3 4 5 6 7 8 9 10 11 |
public class JavaIntegerToStringExample3 { public static void main(String[] args) { Integer val=566787643; int radix=78; String val1=Integer.toString(val,radix); //It returns a string representation of the value of this object in the specified radix. System.out.println("String Value : "+val1); } } |
Output
1 2 3 |
String Value : 566787643 |
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class JavaIntegerToStringExample4 { public static void main(String[] args) { //passing negative value Integer val=-5667; String val1=Integer.toString(val); System.out.println("Unsigned String Value : "+val1); //calling string class methods StringBuffer sf= new StringBuffer(val1); StringBuffer reverse=sf.reverse(); System.out.println("Reverse of "+sf+" : "+reverse); } } |
Output
1 2 3 4 |
Unsigned String Value : -5667 Reverse of 7665- : 7665- |