### Hexadecimal Literals Tutorial

Hexadecimal literals are whole numbers of the base 16 numbering system. They are a sub-grouping of the general category of integer literals.
Hexadecimal literals are whole numbers that can be assigned to variables of the following data types: **byte**, **char**, **short**, **int**, and **long**.
Hexadecimal literals begin with a 0X or a 0x prefix. The Hexadecimal numbering system uses the following numbers and letters: 0123456789ABCDEF.
A,B,C,D,E, and F represent the decimal numbers 10,11,12,13,14, and 15.
The letters A,B,C,D,E, and F can be either uppercase or lowercase - a,b,c,d,e, and f.

This tutorial will teach you how the Hexadecimal numbering system works and how to assign Hexadecimal literals to variables.Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F

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

**
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 Hexadecimal Numbering System

Just like the decimal system, the Hexadecimal system is read from right to left. The rightmost column is 16^0, next is 16^1, next is 16^2, next is 16^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 16^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 16. Add up all the results and you will have your decimal conversion. Here are decimal numbers 0-33 in a Hexadecimal chart:

16^3 | 16^2 | 16^1 | 16^04096 | 256 | 16 | 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 0 89 0 0 0 910 0 0 0 A11 0 0 0 B12 0 0 0 C13 0 0 0 D14 0 0 0 E15 0 0 0 F16 0 0 1 0 0+(1*16)=1617 0 0 1 1 1+(1*16)=1718 0 0 1 2 2+(1*16)=1819 0 0 1 3 3+(1*16)=1920 0 0 1 4 4+(1*16)=2021 0 0 1 5 5+(1*16)=2122 0 0 1 6 6+(1*16)=2223 0 0 1 7 7+(1*16)=2324 0 0 1 8 8+(1*16)=2425 0 0 1 9 9+(1*16)=2526 0 0 1 A 10+(1*16)=2627 0 0 1 B 11+(1*16)=2728 0 0 1 C 12+(1*16)=2829 0 0 1 D 13+(1*16)=2930 0 0 1 E 14+(1*16)=3031 0 0 1 F 15+(1*16)=3132 0 0 2 0 0+(2*16)=3233 0 0 2 1 1+(2*16)=33255 0 0 F F 15+(15*16) = 15+240 = 2554095 0 F F F 15+(15*16)+(15*256)= 15+240+3840 = 409565535 F F F F 15+(15*16)+(15*256)+(15*4096)= 15+240+3840+61440 = 65535

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 HexadecimalLiterals C:\Java>cd HexadecimalLiterals C:\Java\HexadecimalLiterals>Notepad HexadecimalLiterals.java

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

```
class HexadecimalLiterals {
public static void main(String args[]) {
byte a = 0X1;
byte b = 0xA;
short c = 0xf; // 15, note that the f is in lowercase
short d = 0x10; // 16
int e = 0xFF; // 255
int f = 0xFFF; // 4095
int g = 0xFFFF; // 65535
char upperA = 0x41; // 65
char upperB = 0x42; // 66
long m = 0x7FFFFFFFFFFFFFFFL; // 9223372036854775807
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(upperA);
System.out.println(upperB);
System.out.println(m);
}
}
```

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

Now type in **java HexadecimalLiterals** and press Enter.

C:\Java\HexadecimalLiterals>javac HexadecimalLiterals.java C:\Java\HexadecimalLiterals>java HexadecimalLiterals 1 10 15 16 255 4095 65535 A B 9223372036854775807

### Final thoughts

You will see hexadecimal literals used quite often in Java. Especially when assigning a value to a variable of the char data type.