Java – Octal

Octal system is 8 based, digit can be used is from range 0 – 7, no digit can more than 7. Digit right after 7 is 10 or digit right after 17 is 20 etc.

Prefix 0

you can define octal number start from 0. Eg

public static void main(String[] args) {

int oct = 010;

}

Print Decimal Value

public static void main(String[] args) {
int oct = 010;
System.out.println("decimal value = " + oct);
}

Calculation

> 0 1 0
> (0 * 8^2) + (1 * 8^1) + (0 * 8^0)
> 0 + 8 + 0
= 8

Example 2

public static void main(String[] args) {
int first = 010; #8
int second = 022; #18
int total = first + second;
System.out.println("decimal value = " + total); #26
System.out.println("octal = " + Integer.toOctalString(total)); #032
}

Calculation

> 0 2 2
> (0 * 8^2) + (2 * 8^1) + (2 * 8^0)
> 0 + 16 + 2
> 18
Java – Octal

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.