Java – How to declare Hexadecimal literal ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

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

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: