How do I create a NIO Buffer?

This post show you how to use the java.nio buffer such as ByteBuffer, CharBuffer, DoubleBuffer, etc. To create a buffer you can use the allocate(int capacity) method. Each buffer object has the allocate(int capacity) method. When calling this method you need to pass the value of the capacity of the buffer to be created.

package org.kodejava.example.nio;

import java.nio.*;

public class BufferAllocate {
    public static void main(String[] args) {       
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
        CharBuffer charBuffer = CharBuffer.allocate(64);
        ShortBuffer shortBuffer = ShortBuffer.allocate(64);
        IntBuffer intBuffer = IntBuffer.allocate(64);
        LongBuffer longBuffer = LongBuffer.allocate(64);
        FloatBuffer floatBuffer = FloatBuffer.allocate(64);
        DoubleBuffer doubleBuffer = DoubleBuffer.allocate(64);
    }
}

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.