![]() |
VOOZH | about |
The allocateDirect() method of java.nio.ByteBuffer class is used to Allocate a new direct byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. Whether or not it has a backing array is unspecified. This method is 25%-75% faster than the allocate() method.
Syntax:
public static ByteBuffer allocateDirect(int capacity)Parameters: This method takes capacity, in bytes, as a parameter.
Return Value: This method returns the new byte buffer.
Exception: This method throws IllegalArgumentException If the capacity is a negative integer.
Below is the implemented ByteBuffer allocateDirect() method :
State of the ByteBuffer : java.nio.DirectByteBuffer[pos=4 lim=4 cap=4]
Below is the implementation of ByteBuffer allocateDirect() method in Java to show IllegalArgumentException
Trying to allocate negative value in ByteBuffer Exception thrown : java.lang.IllegalArgumentException: capacity < 0: (-4 < 0)