### 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

#### 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 | Ones10^3 | 10^2 | 10^1 | 10^07,491 7 4 9 1832 8 3 241 4 19 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^0514 | 64 | 8 | 1---------------------0 0 0 0 01 0 0 0 12 0 0 0 23 0 0 0 34 0 0 0 45 0 0 0 56 0 0 0 67 0 0 0 78 0 0 1 09 0 0 1 1 1+(1*8)=910 0 0 1 2 2+(1*8)=1011 0 0 1 3 3+(1*8)=1112 0 0 1 4 4+(1*8)=1213 0 0 1 5 5+(1*8)=1314 0 0 1 6 6+(1*8)=1415 0 0 1 7 7+(1*8)=1516 0 0 2 0 0+(2*8)=1617 0 0 2 1 1+(2*8)=1718 0 0 2 2 2+(2*8)=1819 0 0 2 3 3+(2*8)=1920 0 0 2 4 4+(2*8)=2021 0 0 2 5 5+(2*8)=2122 0 0 2 6 6+(2*8)=2223 0 0 2 7 7+(2*8)=2324 0 0 3 0 0+(3*8)=2425 0 0 3 1 1+(3*8)=25146 0 2 2 2 2+(2*8)+(2*64)=146668 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 C:\Java\OctalLiterals>Notepad OctalLiterals.java

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.