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));
Recommended Posts:
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang Integer.toHexString() Method with Examples
- Java lang.Long.toBinaryString() method with Examples
- Java.lang.Long.valueOf() method with examples
- Java.lang.InheritableThreadLocal Class with Examples
- Java.lang.string.replace() method in Java
- Java lang.Integer.toBinaryString() method
- Java.lang.String.lastIndexOf() Method
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.



