Java Long.toString Method

JavaJavaBeginner
Practice Now

Introduction

The Java Long.toString() method is used to convert a Long object to its equivalent string representation. This lab will guide us on how to use the Long.toString() method in our Java programs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/oop -.-> lab-117926{{"`Java Long.toString Method`"}} java/user_input -.-> lab-117926{{"`Java Long.toString Method`"}} java/wrapper_classes -.-> lab-117926{{"`Java Long.toString Method`"}} java/identifier -.-> lab-117926{{"`Java Long.toString Method`"}} java/data_types -.-> lab-117926{{"`Java Long.toString Method`"}} java/operators -.-> lab-117926{{"`Java Long.toString Method`"}} java/output -.-> lab-117926{{"`Java Long.toString Method`"}} java/strings -.-> lab-117926{{"`Java Long.toString Method`"}} java/variables -.-> lab-117926{{"`Java Long.toString Method`"}} java/object_methods -.-> lab-117926{{"`Java Long.toString Method`"}} java/string_methods -.-> lab-117926{{"`Java Long.toString Method`"}} java/system_methods -.-> lab-117926{{"`Java Long.toString Method`"}} end

Declare a Long variable

Declare a variable of type Long and initialize it with a value.

Long longValue = 123456789L;

Convert Long to String

Use the Long.toString() method to convert the Long variable to a string representation.

String strValue = Long.toString(longValue);

Print the String representation

Print the string representation of the Long variable using the System.out.println() method.

System.out.println("String representation of longValue: " + strValue);

Test the program

Save the file and navigate to the ~/project directory in the terminal. Compile the program using the following command:

javac LongToString.java

Execute the program using the following command:

java LongToString

The output should be:

String representation of longValue: 123456789

Test with user input

Update the program to use user input to assign a value to the Long variable.

Scanner scanner = new Scanner(System.in);
Long longValue = scanner.nextLong();
String strValue = Long.toString(longValue);
System.out.println("String representation of longValue: " + strValue);

Test the updated program

Save the updated file and compile the program using the following command:

javac LongToString.java

Execute the program using the following command:

java LongToString

Enter a long value when prompted and verify that the program outputs the correct string representation of that value.

Test with negative value

Update the program to use a negative value and verify that the output is correct.

Long longValue = -123456789L;
String strValue = Long.toString(longValue);
System.out.println("String representation of longValue: " + strValue);

Compile and execute the updated program

Save the updated file and compile the program using the following command:

javac LongToString.java

Execute the program using the following command:

java LongToString

Verify that the program outputs the correct string representation of the negative value.

Test with maximum and minimum values

Update the program to use the maximum and minimum values of a Long and verify that the output is correct.

Long maxValue = Long.MAX_VALUE;
Long minValue = Long.MIN_VALUE;
String strMaxValue = Long.toString(maxValue);
String strMinValue = Long.toString(minValue);
System.out.println("String representation of maxValue: " + strMaxValue);
System.out.println("String representation of minValue: " + strMinValue);

Compile and execute the updated program

Save the updated file and compile the program using the following command:

javac LongToString.java

Execute the program using the following command:

java LongToString

Verify that the program outputs the correct string representation of the maximum and minimum values.

Summary

Congratulations! You have successfully learned how to use the Long.toString() method in your Java programs. The Long.toString() method is a useful utility method to convert a Long object to its string representation.

Other Java Tutorials you may like