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.

    Decimal       0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
    Hexadecimal   0 1 2 3 4 5 6 7 8 9 A  B  C  D  E  F 

This tutorial will teach you how the Hexadecimal numbering system works and how to assign Hexadecimal 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 )
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 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^0  
        4096 | 256  | 16   | 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      0      8  
9       0      0      0      9  
10      0      0      0      A  
11      0      0      0      B  
12      0      0      0      C  
13      0      0      0      D  
14      0      0      0      E  
15      0      0      0      F  
16      0      0      1      0  0+(1*16)=16
17      0      0      1      1  1+(1*16)=17
18      0      0      1      2  2+(1*16)=18
19      0      0      1      3  3+(1*16)=19
20      0      0      1      4  4+(1*16)=20
21      0      0      1      5  5+(1*16)=21
22      0      0      1      6  6+(1*16)=22
23      0      0      1      7  7+(1*16)=23
24      0      0      1      8  8+(1*16)=24
25      0      0      1      9  9+(1*16)=25
26      0      0      1      A  10+(1*16)=26
27      0      0      1      B  11+(1*16)=27
28      0      0      1      C  12+(1*16)=28
29      0      0      1      D  13+(1*16)=29
30      0      0      1      E  14+(1*16)=30
31      0      0      1      F  15+(1*16)=31
32      0      0      2      0  0+(2*16)=32
33      0      0      2      1  1+(2*16)=33
255     0      0      F      F  15+(15*16) = 15+240 = 255
4095    0      F      F      F  15+(15*16)+(15*256)= 15+240+3840 = 4095 
65535   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.


Tutorials