Syntax error Java lang Long.toOctalString() method with examples

Java lang Long.toOctalString() method with examples



In this article, we will learn how to use the java.lang.Long.toOctalString() method in Java. The java.lang.Long.toOctalString() method returns a string representation of the long argument as an unsigned integer in base 8.

Java.lang.Long.toOctalString() method

The java.lang.Long.toOctalString() method converts a given long value into its octal (base-8) string representation. It returns a string that represents the numerical value in octal format. This method is useful when you need to display or work with numbers in octal form.

Steps to convert a long to an octal string

The following are the steps to convert a long to an octal string

Step 1. Declare and Initialize the Long Variable:

  •  Declare a long variable and assign a value to it.

Step 2. Convert and Print the Octal Representation:

  • Use the Long.toOctalString() method to convert the long value to an octal string.

Java program to convert a long value to an octal string

The following is an example of converting a long value to an octal string

import java.lang.*;
public class Main {
   public static void main(String[] args) {
      long l = 220;
      System.out.println("Number = " + l);
      /* returns the string representation of the unsigned long value
      represented by the argument in Octal (base 8) */
      System.out.println("Octal = " + Long.toOctalString(l));
   }
}

Output

Number = 220
Octal = 334

Java program for a negative long value

The following is an example of a Java program for a negative long value.
import java.lang.*;
public class Main {
   public static void main(String[] args) {
      long l = -55;
      System.out.println("Number = " + l);
      System.out.println("Octal = " + Long.toOctalString(l));
   }
}

Output

Number = -55
Octal = 1777777777777777777711

Code Explanation

The Long.toOctalString() method takes a long value and returns its octal (base 8) string representation. For example, when 220 is passed, it converts to "334". When -55 is passed, the method returns "1777777777777777777711", which is the two's complement representation of the negative number in octal.

Updated on: 2025-02-10T11:30:12+05:30

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements