Octal is the base-8 number system and uses the digits 0 to 7 in operation. Octal is widely used in computing on systems like the PDP-8, and IBM mainframes employed 12-bit, 24-bit or 36-bit words.
The Java.lang.Integer.toOctalString() method is a built-in function in Java that is used to return a string representation of the integer argument as an unsigned integer in base 8.
Syntax:
public static String toOctalString(int num)
Parameters : The method accepts a single parameter num of integer type which is requiered to be converted to a string.
Return Value: The function returns a string representation of the integer argument as an unsigned integer in base 8.
Examples:
Consider an integer a = 86 Octal output = 126 Consider an integer a = 186 Octal output = 272
Below programs illustrate the working of Integer.toOctalString() method:
Program 1: For positive integer:
// Java program to demonstrate working // of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = 527; System.out.println("Integral Number = " + a); // returns the string representation of the unsigned int value // represented by the argument in octal (base 8) System.out.println("Octal Number = " + Integer.toOctalString(a)); } } |
Integral Number = 527 Octal Number = 1017
Program 2: For Negative integer:
// Java program to demonstrate working // of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { int a = -51; System.out.println("Integral Number = " + a); // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal Number = " + Integer.toOctalString(a)); } } |
Integral Number = -51 Octal Number = 37777777715
Program 3: For decimal value or a string:
Note: Whenever a decimal number or a string is passed as an argument, it returns an error message which results incompatible types.
// Java program to demonstrate working // of Integer.toOctalString() method import java.lang.*; public class Geeks { public static void main(String[] args) { double a = 18.71; // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal output = " + Integer.toOctalString(a)); String b = "63"; // Returns the string representation of the unsigned int value // in octal (base 8) System.out.println("Octal output = " + Integer.toOctalString(a)); } } |
Output:
prog.java:15: error: incompatible types: possible lossy conversion from double to int
System.out.println("Octal output = " + Integer.toOctalString(a));
^
prog.java:21: error: incompatible types: possible lossy conversion from double to int
System.out.println("Octal output = " + Integer.toOctalString(a));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java lang Long.toOctalString() Method with Examples
- Program to Convert Set of Integer to Array of Integer in Java
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- Integer.valueOf() vs Integer.parseInt() with Examples
- Java.lang.Integer class in Java
- Java Integer compareTo() method
- Java Integer compareUnsigned() method
- Java Integer compare() method
- Java Integer byteValue() method
- Java Integer bitCount() method
- Java lang.Integer.toBinaryString() method
- Java lang Integer.toHexString() Method with Examples
- Integer doubleValue() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer.numberOfTrailingZeros() Method in Java with Example
- Integer hashCode() Method in Java
- Integer decode() Method in Java
- Integer lowestOneBit() Method in Java
- Integer highestOneBit() Method in Java
- Integer reverse() Method In Java
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.

