LAST UPDATED: NOVEMBER 5, 2020
Java Integer toString() Method
Java toString()
method is a part of the Integer
class of the java.lang
package. This method returns the String object with its value equivalent to the signed decimal representation of the integer value passed. This method overrides the toString()
method of the Object
class.
In short, this method is used to convert Integer Object into String.
Syntax:
public String toString()
Parameters:
No parameters are passed in this method.
Returns:
Returns the String representation of the Integer Object.
Example 1:
Here, the Integer objects are converted into its equivalent String representations.
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
Integer a = 50;
String s1 = a.toString(); // return a string value
System.out.println("Equivalent String is " + s1);
Integer b = -18;
String s2 = b.toString(); // return a string value
System.out.println("Equivalent String is " + s2);
}
}
Equivalent String is 50
Equivalent String is -18
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value ");
Scanner sc = new Scanner(System.in);
Integer val = sc.nextInt();
String s = val.toString(); //converting to string
System.out.println("String value is : "+ s);
}
catch(Exception e)
{
System.out.println("Invalid input!!");
}
}
}
Enter the value 20
String value is : 20
************************
Enter the value -53
String value is : -53
************************
Enter the value 0x342
Invalid input!!
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.