Java lang Long.toOctalString() Method with Examples


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

Syntax:

public static String 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 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 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 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 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


The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :



Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.