﻿ Javac · Java Tutorials - Octal Literals Tutorial

Octal Literals Tutorial

Octal literals are whole numbers of the base 8 numbering system. They are a sub-grouping of the general category of integer literals. Octal literals are whole numbers that can be assigned to variables of the following data types: byte, char, short, int, and long. Octal literals begin with a 0 (zero) prefix. The Octal numbering system uses the numbers 0-7, 01234567.

• int a = 03; // decimal 3
• int b = 011; // decimal 9
This tutorial will teach you how the octal numbering system works and how to assign octal literals to variables.

The Caret Symbol ^ and Exponents

Exponents are shorthand for multiplication of the same number over and over and over again. The number of times a number is multiplied by itself is represented following the caret symbol ^.
Here is how it works: (number) (caret ^ also stated as "to the power of") (number of times to repeat multiplication )
3^3 = 3 x 3 x 3 = 27
4^2 = 4 x 4 = 16
5^3 = 5 x 5 x 5 = 125
2^5 = 2 x 2 x 2 x 2 x 2 = 32
Pretty basic, right?

Review of the Decimal System

Everybody knows the decimal system, you can count from 1-100 and then keep on going forever if you wanted to. Try to remember way back to elementary school math, we learned that numbers in the decimal system are organized into columns. Because you have been using the decimal system so long, you might not even realize that the columns are read from right to left. The rightmost column is the ones, next is the tens, next is the hundreds, next is the thousands, and so on an so forth. The decimal system is in fact a base 10 numbering system

```        Thousands | Hundreds | Tens | Ones
10^3      | 10^2     | 10^1 | 10^0
7,491      7           4        9      1
832                    8        3      2
41                              4      1
9                                      9
```

The Octal Numbering System

Just like the decimal system, the octal system is read from right to left. The rightmost column is 8^0, next is 8^1, next is 8^2, next is 8^3, and so on an so forth. You may recall that any number raised to the power of zero is 1. The way it works is your take the number in the rightmost column and multiply that number by 8^0 (which is always 1). Remember that number. You then work your way to the left multiplying whatever number is each column by an ever increasing power of 8. Add up all the results and you will have your decimal conversion. Here are decimal numbers 0-25 in a octal chart:

```        8^3 | 8^2 | 8^1 | 8^0
514 | 64  |  8  |  1
---------------------
0        0     0     0     0
1        0     0     0     1
2        0     0     0     2
3        0     0     0     3
4        0     0     0     4
5        0     0     0     5
6        0     0     0     6
7        0     0     0     7
8        0     0     1     0
9        0     0     1     1  1+(1*8)=9
10       0     0     1     2  2+(1*8)=10
11       0     0     1     3  3+(1*8)=11
12       0     0     1     4  4+(1*8)=12
13       0     0     1     5  5+(1*8)=13
14       0     0     1     6  6+(1*8)=14
15       0     0     1     7  7+(1*8)=15
16       0     0     2     0  0+(2*8)=16
17       0     0     2     1  1+(2*8)=17
18       0     0     2     2  2+(2*8)=18
19       0     0     2     3  3+(2*8)=19
20       0     0     2     4  4+(2*8)=20
21       0     0     2     5  5+(2*8)=21
22       0     0     2     6  6+(2*8)=22
23       0     0     2     7  7+(2*8)=23
24       0     0     3     0  0+(3*8)=24
25       0     0     3     1  1+(3*8)=25
146      0     2     2     2  2+(2*8)+(2*64)=146
668      1     2     3     4  4+(3*8)+(2*64)+(1*512)=668
```

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

```C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md OctalLiterals
C:\Java>cd OctalLiterals
```

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

``````
class OctalLiterals {
public static void main(String args[]) {
byte a = 01;
byte b = 07;
short c = 010; // 8
short d = 020; // 16
int e = 030; // 24
int f = 040; // 32
int g = 041; // 33
int h = 050; // 40
int i = 060; // 48
int j = 070; // 56
int k = 0100; // 64
char upperA = 0101; // 65
char upperB = 0102; // 66
long m = 055555555555555555555L; // 823515360433462125

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(i);
System.out.println(j);
System.out.println(k);
System.out.println(upperA);
System.out.println(upperB);
System.out.println(m);
}
}
``````

Now switch back to the command prompt (CMD) and type in javac OctalLiterals.java and press Enter.
Now type in java OctalLiterals and press Enter.

```
C:\Java\OctalLiterals>javac OctalLiterals.java
C:\Java\OctalLiterals>java OctalLiterals
1
7
8
16
24
32
33
40
48
56
64
A
B
823515360433462125

```

Final thoughts

Octal literals are rarely used in Java, but understanding them will help understand other more commonly used systems such as hexadecimal.