Java Data Type How to - Declare Hexadecimal integer literal








Question

We would like to know how to declare Hexadecimal integer literal.

Answer

Put 0x or 0X in front of the numbers.

Use the letters A to F (or a to f) to represent digits with values 10 to 15, respectively.

public class MainClass {
// ww  w  .  j  a  v  a 2s.  co m
  public static void main(String[] a) {
    int hexValue1 = 0x100;
    int hexValue2 = 0x1234;
    int hexValue3 = 0xDEAF;
    int hexValue4 = 0xCAB;

    System.out.println(hexValue1);
    System.out.println(hexValue2);
    System.out.println(hexValue3);
    System.out.println(hexValue4);
  }

}

The code above generates the following result.

To create a hexadecimal literal of type long, Append L to the literal.

public class MainClass {

  public static void main(String[] a) {
    long hexLongValue = 0x0FL;
    System.out.println(hexLongValue);
  }

}

The code above generates the following result.