Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

nio.IntBuffer: practical reference

nio.IntBuffer: practical reference: IntBuffer is an int-specialized NIO buffer with independent position/limit state and optional array or direct backing.

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

Syntax

java.nio.IntBuffer

IntBuffer is an int-specialized NIO buffer with independent position/limit state and optional array or direct backing.

Use IntBuffer for relative, absolute, or bulk int access with Buffer state, or as a typed view over binary storage.

slice and duplicate share content but have independent position, limit, and mark. A view created from ByteBuffer captures its byte order at creation; later order changes do not retarget it.

A direct or read-only buffer may not expose an accessible int array. Relative access at the limit underflows or overflows, and writes to a read-only view throw ReadOnlyBufferException.

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.IntBuffer.allocate(2);
        buffer.put(7).put(8).flip();
        System.out.println(buffer.get() + buffer.get());
    }
}

Result

15

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

Sources