Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is endianness, which byte order does Java use by default for DataOutputStream and ByteBuffer, and how would you read a little-endian 32-bit int from a file header?

Endianness is the order in which the bytes of a multi-byte number are stored: big-endian puts the most significant byte first, little-endian puts the least significant byte first. Java's DataInputStream and DataOutputStream always use big-endian, and a new ByteBuffer is big-endian until we change it. To read a little-endian int from a file header we wrap the bytes in a ByteBuffer with order(ByteOrder.LITTLE_ENDIAN), reverse a big-endian read with Integer.reverseBytes, or use a little-endian VarHandle over the byte array.

Why this question is asked

As soon as Java code reads a file format it did not define, such as a BMP or WAV header, a ZIP record or a binary log from a C program, byte order stops being trivia. Get it wrong and every number comes out huge and meaningless while single bytes look fine, which confuses people for hours. Interviewers ask it because it tests whether we know what Java's I/O classes assume by default. In this answer, we will define endianness with a concrete number, see which order each Java class uses, read a little-endian int from a header three different ways, and look at native order and why it matters for direct buffers and native code.

1What Endianness Means, With One Number

Memory and files hold bytes, but an int is four bytes. Endianness is simply the convention for which of those four bytes comes first. Take the number 0x12345678, which is 305,419,896 in decimal. Its most significant byte, the one worth the most, is 0x12, and its least significant byte is 0x78.

A big-endian layout writes the bytes in the order we read the number: 12 34 56 78. A little-endian layout writes them backwards: 78 56 34 12. Neither is better; they are just different agreements. Network protocols traditionally use big-endian, which is why it is also called network byte order, while x86 processors and almost all ARM systems in use today are little-endian in memory, so C programs that write their structs straight to disk produce little-endian files.

0x12345678 stored in four bytes
Byte offsetBig-endianLittle-endian
012 (most significant)78 (least significant)
13456
25634
37812
Same number, same four bytes, opposite order. Reading one layout with the other's rule gives 0x78563412.

2Which Order Java Uses by Default

DataOutputStream.writeInt and DataInputStream.readInt always use big-endian, and there is no setting to change that. This was a deliberate choice: Java wanted the same bytes on every platform, and network order was the obvious standard. A ByteBuffer starts as big-endian too, but it has an order() setting that we can switch per buffer.

Java
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new DataOutputStream(bos).writeInt(0x12345678);
System.out.println(HexFormat.of().formatHex(bos.toByteArray())); // 12345678
ByteBuffer bb = ByteBuffer.allocate(4);
System.out.println(bb.order()); // BIG_ENDIAN
System.out.println(ByteOrder.nativeOrder()); // LITTLE_ENDIAN on x86 and ARM

Notice the difference between the last two lines. Java's own default is fixed and platform independent; the native order is whatever the processor uses, and only matters when we share memory with native code.

3Reading a Little-Endian Int From a File Header

Suppose a file header starts with a 4-byte little-endian record count, and the first bytes on disk are 78 56 34 12. Reading it the Java default way gives the wrong number, and any of three techniques gives the right one.

Java
byte[] header = new byte[16];
try (InputStream in = Files.newInputStream(path)) {
in.readNBytes(header, 0, header.length); // header[0..3] = 78 56 34 12
}
int wrong = ByteBuffer.wrap(header).getInt(0); // 2018915346 (0x78563412)
// 1. ByteBuffer with an explicit order
int a = ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN).getInt(0); // 305419896
// 2. A big-endian read, then reverse the bytes
DataInputStream din = new DataInputStream(new ByteArrayInputStream(header));
int b = Integer.reverseBytes(din.readInt()); // 305419896
// 3. A VarHandle that views the byte array as little-endian ints
VarHandle LE_INT = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN);
int c = (int) LE_INT.get(header, 0); // 305419896

The ByteBuffer version is the usual choice for parsing a whole header, because we set the order once and then call getInt, getShort and getLong at known offsets. Integer.reverseBytes (and its cousins Short.reverseBytes and Long.reverseBytes) is handy when we already have a DataInputStream for the rest of the file. The VarHandle reads directly from the byte array with no buffer object at all, which suits hot loops; we keep the VarHandle in a static final field so the JIT can optimise it.

But there is a catch with single bytes. A byte has no order, so header[4] is right whatever the endianness, which is exactly why a byte-order bug shows up as "small values are fine, larger numbers are huge".

4Native Order, Direct Buffers and Native Code

ByteOrder.nativeOrder() matters when Java and native code look at the same memory. A direct ByteBuffer, or a MemorySegment from the Foreign Function and Memory API, lives outside the Java heap and is often passed to C code or mapped from a file. If C writes an int into that memory, it writes it in the processor's order, so Java must read it with buffer.order(ByteOrder.nativeOrder()) or the matching little-endian layout.

For data that crosses machines, such as files and network messages, we should never use native order. A file written with the native order of an x86 server would be read differently on a big-endian machine. For exchange formats we always name the order explicitly in the code and in the format's documentation.

5Follow-up questions

Does endianness affect the bits inside a byte?

No. Endianness is about the order of bytes within a larger value. The bits of a single byte are always interpreted the same way by Java and by the hardware, which is why byte values survive a byte-order mismatch.

Does order() change the existing bytes in a ByteBuffer?

No. It only changes how later getInt, putInt and similar calls interpret or write multi-byte values. The bytes themselves stay exactly where they are.

How would you write a little-endian int?

With ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array(), or with DataOutputStream.writeInt(Integer.reverseBytes(value)), or with the same little-endian VarHandle using set instead of get.

6Conclusion

In this answer, we saw that endianness is only the order of bytes in a multi-byte value, with 0x12345678 stored as 12 34 56 78 in big-endian and 78 56 34 12 in little-endian. Java's DataInputStream and DataOutputStream always use big-endian, and ByteBuffer starts big-endian but can be switched. We read a little-endian header int three ways, with ByteBuffer.order, with Integer.reverseBytes and with a little-endian VarHandle, and saw that native order matters for direct buffers and native interop, not for exchange formats.

What to say in the interviewAbout 70 seconds aloud

Open with

Endianness is the order of bytes in a multi-byte number. For 0x12345678, big-endian stores the most significant byte first, 12 34 56 78, and little-endian stores the least significant first, 78 56 34 12.

Then, if they want more

Networks traditionally use big-endian, and x86 and most ARM processors are little-endian in memory, so files written by C programs on those machines are usually little-endian. In Java, DataInputStream and DataOutputStream always use big-endian, with no option to change it, and a ByteBuffer is big-endian by default but has an order method.

To read a little-endian int from a header, I would read the header bytes and use ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN) .getInt(offset). If I am already using a DataInputStream I can call Integer.reverseBytes on readInt, and for tight loops a VarHandle from MethodHandles.byteArrayViewVarHandle with int[].class and LITTLE_ENDIAN reads straight from the byte array.

ByteOrder.nativeOrder tells me the platform's order, which matters for direct buffers or memory shared with native code, but for files and messages I always state the order explicitly rather than relying on the platform. A typical symptom of getting it wrong is that single bytes look right but larger numbers are huge.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is endianness, which byte order does Java use by default for DataOutputStream and...": https://scrivio-interview-library.netlify.app/java/io-and-nio/40431ba228/