Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

The terms “Big Endian” and “Little Endian” describe the order of arrangement of bytes in memory. Endianness is crucial when dealing with data serialization, network communication, or when reading binary data in different hardware architectures.

In this tutorial, we’ll delve into the specifics of how Java reads integers and whether it follows the Little or Big Endian approach.

2. What Is Endianness?

Endianness refers to the arrangement of bytes in computer memory. It comes in two forms: Little Endian and Big Endian.

Big Endian stores the most significant byte at the smallest memory address.

On the other hand, Little Endian stores the least significant byte at the smallest memory address.

3. Byte Order in Java

Let’s see a sample code that outputs the byte order of an integer value:

int value = 123456789;
byte [] bytes = ByteBuffer.allocate(4).putInt(value).array();
for (byte b : bytes) {
    System.out.format("0x%x ", b);
}

In the code above, we declare an integer variable named value. Next, we declare a byte array and allocate space for 4 bytes by invoking the allocate() method on ByteBuffer.

Furthermore, we invoke the putInt() method to write four bytes of data into the ByteBuffer object. Additionally, the array() method helps get the byte array from the ByteBuffer object.

Finally, we print the hexadecimal value of each byte.

Let’s see the output of the sample code:

0x7 0x5b 0xcd 0x15 

The output above shows the order of four bytes representing the integer in memory. The first byte in the output “0x7” is the most significant byte of the integer, and the last byte “0x15” is the least significant. This order of bytes, from most significant to least significant, is a characteristic of Big Endian byte order.  Therefore, we can infer that Java, by default, uses Big Endian byte order for integers.

However, most processors orders byte in Little Endian format. During runtime, the JVM uses the host machine’s native Endianness when reading binary data.

Java provides the flexibility to read data in Little Endian order through the ByteBuffer class. Let’s modify the sample code to use the native Endianness of the host machine:

int value = 123456789;
byte [] bytes = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(value).array();
for (byte b : bytes) {
    System.out.format("0x%x ", b);
}

Here, we modify the sample code to use the host machine Endianness by invoking the order() method.

Let’s see the new output:

0x15 0xcd 0x5b 0x7 

The byte order is re-arranged from the least significant byte “0x15” to the most significant byte “0x7“. This shows that the host machine Endianness is Little Endian.

4. Conclusion

In this article, we learned that Java defaults to Big Endian byte order. Additionally, Java provides the necessary class to read data in Little Endian when necessary.

As always, the complete source code for the example is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.