Java – How to declare Hexadecimal literal ?

Problem statement

You need to declare an Hexadecimal literal in Java and display the result on a console window.

Solution

Hexadecimal numbers can contain numbers from 0-9 as well as characters A-F where A-F represents 10-15.

You can prefix the numbers with 0X to denote it as hexa decimal number as shown below.

public class Main {

    public static void main(String[] args) {
        int hexadecimalac1 = 0XA;
        int hexadecimalac2 = 0X15;
        System.out.println(hexadecimalac1);
        System.out.println(hexadecimalac2);
        
    }
}

The output of the above program will be

10

21

Leave Your Comment

Liked this content?. Please spread the word :)