Java Long toString Exploration

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the toString() method of the Long class in Java. We will look into the syntax of this method and understand the parameters it takes and the values it returns. Finally, we will write and execute some example programs to understand the working of the toString() method.


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/inner_classes("`Inner Classes`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") 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-117930{{"`Java Long toString Exploration`"}} java/inner_classes -.-> lab-117930{{"`Java Long toString Exploration`"}} java/classes_objects -.-> lab-117930{{"`Java Long toString Exploration`"}} java/class_methods -.-> lab-117930{{"`Java Long toString Exploration`"}} java/exceptions -.-> lab-117930{{"`Java Long toString Exploration`"}} java/modifiers -.-> lab-117930{{"`Java Long toString Exploration`"}} java/oop -.-> lab-117930{{"`Java Long toString Exploration`"}} java/packages_api -.-> lab-117930{{"`Java Long toString Exploration`"}} java/user_input -.-> lab-117930{{"`Java Long toString Exploration`"}} java/wrapper_classes -.-> lab-117930{{"`Java Long toString Exploration`"}} java/identifier -.-> lab-117930{{"`Java Long toString Exploration`"}} java/arrays -.-> lab-117930{{"`Java Long toString Exploration`"}} java/comments -.-> lab-117930{{"`Java Long toString Exploration`"}} java/data_types -.-> lab-117930{{"`Java Long toString Exploration`"}} java/operators -.-> lab-117930{{"`Java Long toString Exploration`"}} java/output -.-> lab-117930{{"`Java Long toString Exploration`"}} java/strings -.-> lab-117930{{"`Java Long toString Exploration`"}} java/variables -.-> lab-117930{{"`Java Long toString Exploration`"}} java/object_methods -.-> lab-117930{{"`Java Long toString Exploration`"}} java/string_methods -.-> lab-117930{{"`Java Long toString Exploration`"}} java/system_methods -.-> lab-117930{{"`Java Long toString Exploration`"}} end

Import required package and class

In order to use the Long class and its method, we need to import the package java.lang. The Long.toString() method returns the String representation of a long value with respect to the radix provided. In this step, we will import the package java.lang and create a class LongToString of public type.

import java.lang.*;

public class LongToString {
    // class code
}

Create a method to demonstrate converting long to string

In this step, we will create a public static method convertLongToString() inside the LongToString class. This method will take two parameters - a long value l and a radix value r which define the base for the String conversion. Finally, the method will return a String value which contains the string representation of the long value.

public static String convertLongToString(long l, int r) {
    String s = Long.toString(l, r);
    return s;
}

Call convertLongToString() method with different values

In this step, we will call the convertLongToString() method multiple times with different long and radix values. We will print the returned string to the console.

public static void main(String[] args) {
    // using decimal radix (10)
    long l1 = 2584L;
    int r1 = 10;
    System.out.println("Decimal Equivalent of " + l1 + ": " + convertLongToString(l1, r1)); //  output: "Decimal Equivalent of 2584: 2584"

    // using hexadecimal radix (16)
    long l2 = 255L;
    int r2 = 16;
    System.out.println("Hexadecimal Equivalent of " + l2 + ": " + convertLongToString(l2, r2)); // output: "Hexadecimal Equivalent of 255: ff"

    // using octal radix (8)
    long l3 = 133;
    int r3 = 8;
    System.out.println("Octal Equivalent of " + l3 + ": " + convertLongToString(l3, r3)); //output: "Octal Equivalent of 133: 205"
}

Code to handle Exceptions

In the preceding steps, we did not handle the exception when an invalid input is provided from the user. In this step, we will handle any exception by using the try-catch block.

import java.util.Scanner;
import java.lang.*;

public class LongToString {
    public static String convertLongToString(long l, int r) {
        String s = Long.toString(l, r);
        return s;
    }

    public static void main(String[] args) {
        try {
            System.out.println("Enter a long value and a radix: ");
            Scanner sc = new Scanner(System.in);
            long num = sc.nextLong();
            int radix = sc.nextInt();
            System.out.println(convertLongToString(num, radix));
        } catch (Exception e) {
            System.out.println("Invalid input. " + e.getMessage());
        }
    }
}

Compile and execute the code

When we run the code, we can call the main() method of the LongToString class. It will call the convertLongToString() method to perform string conversion. We can compile the Java code and execute it in the terminal. To do this, follow the below steps:

  1. Open terminal
  2. Navigate to the directory where the LongToString.java file is stored using the cd command.
  3. Compile the code using the javac LongToString.java command and hit Enter.
  4. Execute the code using the java LongToString command and hit Enter.
  5. When the code runs, it will prompt the user to enter the long value and the radix value in separate input lines.
  6. Once the user enters the values, the program will output the String equivalent of the entered long value with respect to the entered radix.
$ cd ~/project
$ javac LongToString.java
$ java LongToString
Enter a long value and a radix:
377
16
17f

Summary

In this lab, we learned about the Long.toString() method of the Long class in Java. We learned about the syntax of the toString() method and the parameters it takes. We also wrote and executed some example programs where the toString() method was used to convert long values to their equivalent String representations with respect to a given radix value. We also learned how to handle exceptions while getting input from the user.

Other Java Tutorials you may like