Open In App

Java lang Long.toOctalString() Method with Examples

Last Updated : 19 Nov, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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
// Java program to demonstrate working
// of java.lang.Long.toOctalString() method
import java.lang.Math;
 
class Gfg1 {
 
    // driver code
    public static void main(String args[])
    {
 
        long l = 16;
 
        // returns the string representation of the unsigned int value
        // represented by the argument in octal (base 8)
        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
// Java program to demonstrate 
// of java.lang.Long.toOctalString() method
// negative number 

import java.lang.Math;

class Gfg1 {

    // driver code
    public static void main(String args[])
    {
        // when negative number is passed
    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
// Java program to demonstrate 
// of java.lang.Long.toOctalString() method
// string number 

import java.lang.Math;

class Gfg1 {

    // driver code
    public static void main(String args[])
    {
        // when string number is passed
    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
 // Java program to demonstrate 
// of java.lang.Long.toOctalString() method
// decimal number 

import java.lang.Math;

class Gfg1 {

    // driver code
    public static void main(String args[])
    {
        // when decimal number is passed
    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)); 


 


Next Article
Practice Tags :

Similar Reads