Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Long toString(long i) Method

Java toString() method is a part of the Long class of the java.lang package. This method returns the equivalent String object of the long value passed as an argument i.e it converts the passed long value into a signed decimal representation and returns it as a String.

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

Syntax:

public static String toString(long i)  

Parameter:

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

Returns:

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

Example 1:

Here, the long values are converted into its equivalent signed decimal representations and returned as a String.

import java.lang.Long;

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		long a = 40L;
		long b = -56L;
		System.out.println("Equivalent String is : "+Long.toString(a));   
		System.out.println("Equivalent String is : "+Long.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);  
			long val = sc.nextLong();  
			String s = Long.toString(val); //converting to string
			System.out.println("String value is : "+ s);  
		}
		catch(Exception e)
		{
			System.out.println("Invalid input!!");
		}
	}  
}


Enter the value 7554
String value is : 7554
***************************
Enter the value -6990
String value is : -6990
***************************
Enter the value 0x690
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.