Convert Long To String

JavaJavaBeginner
Practice Now

Introduction

The Long toString(long i) method is used to convert a long value to a String object. This method returns a String that represents the signed decimal integer value of the given argument. The method is static, so it can be accessed with the class name Long and no object of the Long class is required to use it.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/format("`Format`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") 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/scope -.-> lab-117928{{"`Convert Long To String`"}} java/format -.-> lab-117928{{"`Convert Long To String`"}} java/classes_objects -.-> lab-117928{{"`Convert Long To String`"}} java/class_methods -.-> lab-117928{{"`Convert Long To String`"}} java/modifiers -.-> lab-117928{{"`Convert Long To String`"}} java/oop -.-> lab-117928{{"`Convert Long To String`"}} java/packages_api -.-> lab-117928{{"`Convert Long To String`"}} java/user_input -.-> lab-117928{{"`Convert Long To String`"}} java/wrapper_classes -.-> lab-117928{{"`Convert Long To String`"}} java/identifier -.-> lab-117928{{"`Convert Long To String`"}} java/arrays -.-> lab-117928{{"`Convert Long To String`"}} java/comments -.-> lab-117928{{"`Convert Long To String`"}} java/data_types -.-> lab-117928{{"`Convert Long To String`"}} java/operators -.-> lab-117928{{"`Convert Long To String`"}} java/output -.-> lab-117928{{"`Convert Long To String`"}} java/strings -.-> lab-117928{{"`Convert Long To String`"}} java/variables -.-> lab-117928{{"`Convert Long To String`"}} java/object_methods -.-> lab-117928{{"`Convert Long To String`"}} java/string_methods -.-> lab-117928{{"`Convert Long To String`"}} java/system_methods -.-> lab-117928{{"`Convert Long To String`"}} end

Write the Java code

Write the below Java code in the LongToString.java file. The code will contain a main() method, which will use the Long.toString() method to convert the long value to String.

public class LongToString {
    public static void main(String[] args) {
        // Declaring and initializing a long variable
        long number = 1234567890L;

        // Converting the long value to String
        String strValue = Long.toString(number);

        // Printing the value before and after conversion
        System.out.println("Long value: " + number);
        System.out.println("String value: " + strValue);
    }
}

Save and Compile the code

Save the file and compile the code using the terminal. Change your directory to ~/project and execute the following command:

javac LongToString.java

Run the code

After a successful compilation, run the code using the following command:

java LongToString

The above command will execute the code, and you will see the output as follows:

Long value: 1234567890
String value: 1234567890

User Input

Modify the code to allow the user to input the value of the long variable, which is to be converted to a String. To achieve this, add the following code to the main() method.

import java.util.Scanner;

public class LongToString {
    public static void main(String[] args) {
        // Creating Scanner object
        Scanner scanner = new Scanner(System.in);

        // Prompting user to enter the value of the long variable
        System.out.print("Enter the value of the long variable: ");

        // Reading the user input
        long number = scanner.nextLong();

        // Converting the long value to String
        String strValue = Long.toString(number);

        // Printing the value before and after conversion
        System.out.println("Long value: " + number);
        System.out.println("String value: " + strValue);
    }
}

Save and Compile the code

Save the file and compile the code using the terminal. Change your directory to ~/project and execute the following command:

javac LongToString.java

Run the code

After a successful compilation, run the code using the following command:

java LongToString

The above command will execute the code, and you will be prompted to enter the value of the long variable. After entering the value, the program will convert the long value to String, and you will see the output as follows:

Enter the value of the long variable: 5463728190
Long value: 5463728190
String value: 5463728190

Using radix and padding

Modify the code to demonstrate the use of the radix and padding options in the Long.toString() method. To achieve this, replace the existing code with the following code.

import java.util.Scanner;

public class LongToString {
    public static void main(String[] args) {
        // Creating Scanner object
        Scanner scanner = new Scanner(System.in);

        // Prompting user to enter the value of the long variable
        System.out.print("Enter the value of the long variable: ");

        // Reading the user input
        long number = scanner.nextLong();

        // Converting the long value to String
        String strValue = Long.toString(number, 16);
        String paddedStrValue = String.format("%016x", number);

        // Printing the value before and after conversion
        System.out.println("Long value: " + number);
        System.out.println("String value with radix 16: " + strValue);
        System.out.println("Padded String value with radix 16: " + paddedStrValue);
    }
}

In the above code, we are using the radix option to convert the long value to hexadecimal format, and the padding option to ensure that the output string is of fixed length.

Save and Compile the code

Save the file and compile the code using the terminal. Change your directory to ~/project and execute the following command:

javac LongToString.java

Run the code

After a successful compilation, run the code using the following command:

java LongToString

The above command will execute the code, and you will be prompted to enter the value of the long variable. After entering the value, the program will convert the long value to String and display the value with radix 16 and padding options, and you will see the output as follows:

Enter the value of the long variable: 5463728190
Long value: 5463728190
String value with radix 16: 1432b4a8e
Padded String value with radix 16: 0000001432b4a8e

Summary

The Long.toString(long i) method is used to convert a long value to a String object. In this lab, you have learned how to use the Long.toString() method with different examples. You also learned about the radix and padding options of the Long.toString() method. This method can be very useful to convert a long value to a String object in Java.