Java Long toOctalString Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the toOctalString() method that is part of the Long class in Java. This method is used to convert a long integer to its equivalent octal representation. We will learn how to use this method through some examples.


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/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/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/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/classes_objects -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/class_methods -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/modifiers -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/oop -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/packages_api -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/user_input -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/wrapper_classes -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/identifier -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/arrays -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/data_types -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/operators -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/output -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/strings -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/variables -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/string_methods -.-> lab-117924{{"`Java Long toOctalString Method`"}} java/system_methods -.-> lab-117924{{"`Java Long toOctalString Method`"}} end

Create a new Java Class

Create a new Java class named LongToOctal in the ~/project directory using the following command in the terminal:

cd ~/project
touch LongToOctal.java

Add the import statement

In the LongToOctal class file, add the following import statement at the beginning of the file:

import java.lang.Long;

We need to import the Long class from the java.lang package because the toOctalString() method is part of this class.

Use toOctalString() method in your program

Add the following code inside the main() method to use the toOctalString() method:

long number = 128L;
String octal = Long.toOctalString(number);
System.out.println("Octal representation of " + number + ": " + octal);

This code initializes a long integer value number to 128 and then uses the toOctalString() method to convert it to its equivalent octal representation. The resulting octal representation is then printed to the console.

Use negative numbers

Now, let's modify the program to use negative numbers:

long negativeNumber = -100L;
String octal2 = Long.toOctalString(negativeNumber);
System.out.println("Octal representation of " + negativeNumber + ": " + octal2);

In this code, we initialize negativeNumber to -100 and use the toOctalString() method to convert it to its equivalent octal representation. The resulting octal representation is then printed to the console.

User Input

Finally, let's modify the program to allow user input:

import java.util.Scanner;

public class LongToOctal {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a long integer: ");
        long input = sc.nextLong();
        String octal = Long.toOctalString(input);
        System.out.println("Octal representation of " + input + ": " + octal);
    }
}

Here, we first import the Scanner class to read user input from the console. The program prompts the user to enter a long integer, reads the input using the Scanner class, and then uses the toOctalString() method to convert it to its equivalent octal representation. The resulting octal representation is then printed to the console.

Compile and Run

In the terminal, navigate to the ~/project directory and run the following command to compile the LongToOctal class:

javac LongToOctal.java

Once the compilation succeeds you can run the program using the following command:

java LongToOctal

You will see the program prompt you to enter a long integer. Once you enter a number and press enter, the program will show the octal representation of the input.

Summary

In this lab, we learned about the toOctalString() method that is part of the Long class in Java. We learned how to use this method to convert long integers to their equivalent octal representation. We also learned how to write a Java program that uses this method to convert user input to its equivalent octal representation.

Other Java Tutorials you may like