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.

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.

Other Java Tutorials you may like