java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a NIO Buffer?

To create a buffer you can use the allocate(int capacity) method call. 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.

The example below show you how to create the buffer object by calling its allocate() method.

package com.javacoderanch.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);