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.
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.
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:
Integral Number = 527 Octal Number = 1017
Program 2: For Negative integer:
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.
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 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.