Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

nio.ByteOrder: practical reference

nio.ByteOrder: practical reference: ByteOrder represents BIG_ENDIAN and LITTLE_ENDIAN ordering for multibyte buffer access.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.nio.ByteOrder

ByteOrder represents BIG_ENDIAN and LITTLE_ENDIAN ordering for multibyte buffer access.

Use ByteOrder to state the endianness of multibyte values in a ByteBuffer or typed view according to the file or protocol specification.

nativeOrder is a hardware optimization hint, not a portable wire-format choice. A newly created ByteBuffer starts in BIG_ENDIAN unless its order is changed.

BIG_ENDIAN stores the most significant byte first and LITTLE_ENDIAN the least significant byte first. Single-byte values are unaffected by byte order.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var buffer = java.nio.ByteBuffer.allocate(2).order(java.nio.ByteOrder.LITTLE_ENDIAN);
        buffer.putShort((short) 0x0102).flip();
        System.out.println(buffer.get() & 0xff);
    }
}

Result

2

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources