Java lang Long.toOctalString() Method with Examples (original) (raw)

Last Updated : 19 Nov, 2020

The Java.lang.Long.toOctalString() function is a built-in function in Java that returns a string representation of the long argument as an unsigned integer in base 8.

Syntax:

public static int toOctalString(long num)

Parameters: The function accepts a single mandatory parameter num, which is to be converted to a string. The parameter is of
long data-type.
Returns: The function returns a string representation of the long argument as an unsigned integer in base 8.

Examples:

Input : 16 Output : 20

Input : 1234 Output : 2322

Program to demonstrate the working of function:

Java

import java.lang.Math;

class Gfg1 {

`` public static void main(String args[])

`` {

`` long l = 16 ;

`` System.out.println( "Octal string is " + Long.toOctalString(l));

`` l = 1234 ;

`` System.out.println( "Octal string is " + Long.toOctalString(l));

`` }

}

Output:

Octal string is 20 Octal string is 2322

Program 2: The program below demonstrates the working function when a negative number is passed.

Java

import java.lang.Math;

class Gfg1 {

`` public static void main(String args[])

`` {

`` System.out.println( "Hex is " + Long.toOctalString(- 14 ));

`` }

}

Output:

Hex is 1777777777777777777762

Errors and Exception: Whenever a decimal number or a string is passed as an argument, it returns an error message which says “incompatible types”.

Program 3: The program below demonstrates the working function when a string number is passed.

Java

import java.lang.Math;

class Gfg1 {

`` public static void main(String args[])

`` {

`` System.out.println( "Hex is " + Long.toOctalString( "14" ));

`` }

}

Output:

prog.java:13: error: incompatible types: String cannot be converted to long System.out.println("Hex is " + Long.toOctalString("14"));

Program 4: The program below demonstrates the working function when a decimal is passed.

Java

import java.lang.Math;

class Gfg1 {

`` public static void main(String args[])

`` {

`` System.out.println( "Hex is " + Long.toOctalString( 15.67 ));

`` }

}

Output:

prog.java:13: error: incompatible types: possible lossy conversion from double to long System.out.println("Hex is " + Long.toOctalString(15.67));