Java SE API

Direct buffer allocation

Allocate a direct byte buffer; backing storage may be outside the heap.

Formal Syntax & Structure

java syntaxjava
ByteBuffer.allocateDirect(capacity)

Working Example

Example Snippetjava
public class Main {
    public static void main(String[] args) throws Exception {        var heap = java.nio.ByteBuffer.allocate(16);        var direct = java.nio.ByteBuffer.allocateDirect(16);        heap.putInt(7); direct.putInt(7);        System.out.println(heap.isDirect());        System.out.println(direct.isDirect());        System.out.println(direct.getInt(0));    }}
Java Reference