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.

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