Java - Long toString() method



Description

The Java Long toString() method returns a String object representing this Long's value.

Declaration

Following is the declaration for java.lang.Long.toString() method

 public String toString() 

Parameters

NA

Return Value

This method returns a string representation of the value of this object in base 10.

Exception

NA

Getting String Representation of a Positive long Value Example

The following example shows the usage of Long toString() method to get the string representation of the specified long value. We've created an Long object using a positive long value. Then using toString() method, we're getting the string representation in a variable and printing it.

 package com.tutorialspoint; public class LongDemo { public static void main(String[] args) { Long i = new Long(20L); // returns a string representation of the long value in base 10 String retval = i.toString(); System.out.println("Value = " + retval); } } 

Output

Let us compile and run the above program, this will produce the following result −

 Value = 20 

Getting String Representation of a Negative long Value Example

The following example shows the usage of Long toString() method to get the string representation of the specified long value. We've created an Long object using a negative long value. Then using toString() method, we're getting the string representation in a variable and printing it.

 package com.tutorialspoint; public class LongDemo { public static void main(String[] args) { Long i = new Long(-20L); // returns a string representation of the long value in base 10 String retval = i.toString(); System.out.println("Value = " + retval); } } 

Output

Let us compile and run the above program, this will produce the following result −

 Value = -20 

Getting String Representation of a Zero Value Example

The following example shows the usage of Long toString() method to get the string representation of the specified long value. We've created an Long object using a zero value. Then using toString() method, we're getting the string representation in a variable and printing it.

 package com.tutorialspoint; public class LongDemo { public static void main(String[] args) { Long i = new Long(0L); // returns a string representation of the long value in base 10 String retval = i.toString(); System.out.println("Value = " + retval); } } 

Output

Let us compile and run the above program, this will produce the following result −

 Value = 0 
java_lang_long.htm
Advertisements