Open In App

Integer toOctalString() Method in Java

Last Updated : 19 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 required 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
// 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));
    }
}

Output: 
Integral Number = 527
Octal Number = 1017

 

Program 2: For Negative integer: 

Java
// 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));
    }
}

Output: 
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
// 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 

Next Article
Practice Tags :

Similar Reads