Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Java lang Long.toOctalString() Method with Examples

  • 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:  

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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)); 



 




My Personal Notes arrow_drop_up
Recommended Articles
Page :