How to identify endianness of a system

Though Java is platform independent, there are times when we need to identify the endianness of a system.

In this short blog, we will see how can this be achieved.

1. Using System.Properties()

System.getProperty(“sun.cpu.endian”);

Ex:

System.out.println(“Endianness: ” + System.getProperty(“sun.cpu.endian”));

Output:

Endianness: little

2. Using ByteOrder.nativeOrder()

System.out.println(“ByteOrder: ” + ByteOrder.nativeOrder());
if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
System.out.println(“Little Indian”);
}

Output:

ByteOrder: LITTLE_ENDIAN
Little India

3. Using BitShift

int right = 0;
int left = 1;
int combine = (left<<16) | right;
if(combine == 65536) {
System.out.println(“Little Indian”);
}

Output:

Little Indian