Signup/Sign In

Java Integer toString(int i) Method

Java toString() method is a part of the Integer class of the java.lang package. This method returns the equivalent String object of the integer value passed as an argument.

In short, this method is used to convert integer value into String.

Syntax:

public static String toString(int i)  

Parameter:

The parameter passed is the integer value whose equivalent String is to be returned.

Returns:

Returns the String equivalent of the integer value passed as a parameter.

Example 1:

Here, the integer values are converted into its equivalent String representations.

import java.lang.Integer;
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        int a = 40;
        int b = -56;
        System.out.println("Equivalent String is : "+Integer.toString(a));   
        System.out.println("Equivalent String is : "+Integer.toString(b));   
    }  
}  


Equivalent String is : 40
Equivalent String is : -56

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);  
            int val = sc.nextInt();  
            String s = Integer.toString(val); //converting to string
            System.out.println("String value is : "+ s);          
        }
        catch(Exception e)
        {
            System.out.println("Invalid input!!");
        }
    }  
}  


Enter the value 78
String value is : 78
************************
Enter the value -55
String value is : -55
************************
Enter the value 0x412
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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.