Signup/Sign In
LAST UPDATED: NOVEMBER 24, 2020

Java Long toString() Method

Java toString() method is a part of the Long class of the java.lang package. This method returns the String object with its value equivalent to the signed decimal representation of the long value passed.This method overrides the toString() method of the Object class.

In short, this method is used to convert Long Object into String

Syntax:

public String toString()  

Parameters:

No parameters are passed in this method.

Returns:

Returns the String representation of the Long Object.

Example 1:

Here, the Long objects are converted into signed decimal representation and returned as a String.

import java.lang.Long;

public class StudyTonight
{  
	public static void main(String[] args) 
	{  
		Long a = 50L;       
		String s1 = a.toString();  // return a string value 
		System.out.println("Equivalent String is " + s1);              

		Long b = -18L;            
		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);  
			Long val = sc.nextLong();  
			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 755
String value is : 755
***************************
Enter the value -5889
String value is : -5889
***************************
Enter the value 0x588
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.