Java Reference
In-Depth Information
if (b.equals(ByteOrder.BIG_ENDIAN)) {
System.out.println("Big endian");
}
else {
System.out.println("Little endian");
}
}
}
Little endian
Byte Buffer and Its Byte Order
A byte order is the order in which bytes of a multi-byte value are stored. Suppose you have a short value
300
stored in a
variable as follows:
short s = 300;
A short value is stored in two bytes. The value
300
can be represented in 16-bits as
0000000100101100
, where
the rightmost bit is the least significant bit and the leftmost bit is the most significant bit. You can split the 16-bit
into two bytes as
00000001
and
00101100
. At the byte level, you can think of
00000001
as the most significant byte
and
00101100
as the least significant byte. If you consider two bytes separately for a short value, you may store them
as either
00000001
followed by
00101100
or
00101100
followed by
00000001
. As long as you know the order of the
bytes in which they are stored, you would be able to compute the correct value
300
using either form of the 16-bits:
0000000100101100
or
0010110000000001
.
A byte order is called
big endian
if the bytes of a multi-bytes value are stored from the most significant byte to the
least significant byte. If the bytes of a multi-byte value are stored from the least significant byte to the most significant
byte, it is known as little endian. To remember the two definitions easily, you can replace the word “big” with
“most significant,” “little” with “least significant,” and “endian” with “first”. That is, remember “big endian” as
“most significant first” and “little endian” as “least significant first.”
If you store a short value of
300
as
0000000100101100
, you are using the big endian byte order. In the little endian
byte order, you would store
300
as
0010110000000001
, which seems backwards for representing a 16-bit value.
When you deal with byte data in a byte buffer, you may be considering each byte as an independent byte. A byte
in a byte buffer may be part of a bigger value. When a byte value in a byte buffer is independent, the byte order is not a
consideration. When a byte in a byte buffer is part of a bigger value (e.g. two bytes of a short value
300
), the byte order
becomes very important in reading. If you read two bytes from a byte buffer to compute a short value, you must know
how those two bytes are stored. Suppose you read two bytes as
0000000100101100
. If it is in a big endian byte order, it
represents a value
300
. If it is in a little endian byte order, it represents a value of
11265
.
Java uses a big-endian byte order to store data. By default, a byte buffer uses a big endian byte order. An instance
of the
java.nio.ByteOrder
class represents a byte order. You will not need to instantiate this class because you
always use the value that represents a byte order; you don't create a new byte order. In fact, this class has no
public
constructor. You can use two constants,
BIG_ENDIAN
and
LITTLE_ENDIAN
, which are defined in the
ByteOrder
class to
represent these byte orders.
■
a byte order is meaningful only in a multi-byte value stored in a byte buffer. You may also need to deal with byte
orders when you are dealing with two different systems that use different byte orders.
Tip