Java Integer toOctalString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.toOctalString(n) returns the value of n in base 8, using only the digits 0 to 7. Like the binary and hex versions, it treats the number as an unsigned 32-bit value and adds no prefix.

Octal is still common for Unix file permissions, where a mode such as 755 packs three groups of three bits.

A static method of java.lang.Integer: call it on the class, as Integer.toOctalString(...). Integer lives in java.lang, so no import is needed.

Syntax

One argument: the int to show.

Java
String a = Integer.toOctalString(8);      // "10"
String b = Integer.toOctalString(493);    // "755"
String c = Integer.toOctalString(-1);     // "37777777777"
CallWhat it does
Integer.toOctalString(n)Octal text of the unsigned 32-bit value.
Integer.toString(n, 8)Signed octal text, such as "-10" for -8.
Integer.parseInt(text, 8)The reverse: reads octal text back into an int.
int mode = 0755;An octal literal: a leading zero makes the number octal in source code.

Each octal digit stands for exactly three bits, so a 32-bit value needs up to 11 digits, and the first digit of a negative number is 2 or 3.

Library declarations
Java
// java.lang.Integer
public static String toOctalString(int i)

Parameters

ParameterDescription
i (int)The number to show. Negative values are read as unsigned 32-bit numbers.

Return Value

Return ValueDescription
StringThe octal digits, 1 to 11 characters, without a prefix, sign, or leading zeros.

Compare with Similar Methods

Related conversions:

MethodHow it differs
toBinaryString(n)Shows every bit; octal groups them three at a time.
toHexString(n)Groups the bits four at a time.
toString(n, 8)Signed octal text with a minus sign.
decode("0755")Reads text with a leading zero as octal.

Java Examples

Example 1 — Convert Numbers to Octal

Print a few numbers in octal and see the pattern of powers of 8.

Java
public class IntegerToOctalDemo {
    public static void main(String[] args) {
        int[] numbers = {0, 7, 8, 64, 100, 511};
        for (int n : numbers) {
            System.out.println(n + " -> " + Integer.toOctalString(n));
        }
    }
}

How It Works

  1. Digits stop at 7. 7 is still one digit, but 8 rolls over to 10, just as 10 follows 9 in decimal.
  2. Powers of 8. 64 is 8 × 8, so it is 100.
  3. Mixed numbers. 100 is 1 × 64 + 4 × 8 + 4, which is 144.
  4. All sevens. 511 is 8 × 8 × 8 minus 1, which is 777, the largest three-digit octal number.

Example 2 — Show Unix File Permissions in Octal

Write a permission mode as an octal literal and print it back in the form chmod uses.

Java
public class IntegerToOctalPermissions {
    public static void main(String[] args) {
        int mode = 0755;   // an octal literal: leading zero

        System.out.println("as decimal:   " + mode);
        System.out.println("as octal:     " + Integer.toOctalString(mode));
        System.out.println("parsed back:  " + Integer.parseInt("755", 8));

        int[] modes = {0644, 0600, 0777};
        for (int m : modes) {
            System.out.println(m + " is chmod " + Integer.toOctalString(m));
        }
    }
}

How It Works

  1. A literal with a leading zero. 0755 in source code is octal, so the stored number is 7 × 64 + 5 × 8 + 5, which is 493.
  2. Back to octal text. toOctalString(493) returns 755, the digits people type after chmod.
  3. Parsing with radix 8. parseInt("755", 8) returns 493 again, so the two conversions are inverses.
  4. Other modes. 420 prints as 644, 384 as 600, and 511 as 777.

Example 3 — See Negative Numbers in Octal

Print negative numbers and the limits of int in octal.

Java
public class IntegerToOctalNegative {
    public static void main(String[] args) {
        System.out.println("-1:        " + Integer.toOctalString(-1));
        System.out.println("-8:        " + Integer.toOctalString(-8));
        System.out.println("MAX_VALUE: " + Integer.toOctalString(Integer.MAX_VALUE));
        System.out.println("MIN_VALUE: " + Integer.toOctalString(Integer.MIN_VALUE));
        System.out.println("signed -8: " + Integer.toString(-8, 8));
    }
}

How It Works

  1. Eleven digits. 32 bits do not divide evenly into groups of 3, so the first digit covers only two bits and is at most 3.
  2. -1 is all ones. Every group is 7, and the leading group of two ones is 3, so the text is 37777777777.
  3. -8 ends in 0. Its lowest three bits are 000, so only the last digit changes: 37777777770.
  4. The limits. MAX_VALUE starts with 1 (the top bit is off), and MIN_VALUE is a single 2 followed by ten zeros, while the signed form simply prints -10.

Frequently Asked Questions

Call Integer.toOctalString(n). For example, Integer.toOctalString(8) returns "10".
A number that starts with 0 in source code is an octal literal. 010 means 1 times 8 plus 0, which is 8.
Use Integer.parseInt(text, 8), or Integer.decode("0755") when the text has a leading zero.
toOctalString shows the unsigned bit pattern, while toString(n, 8) uses a minus sign for negative numbers.
Mostly for Unix file permissions such as 755 or 644. For everything else, binary and hex are more common.

Next: toString()

Convert an int to text, in base 10 or any base.

toString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful