ShortBuffer allocate() method in Java With Examples
The allocate() method of java.nio.ShortBuffer Class is used to allocate a new short buffer.
The position of the new Buffer will be zero & it’s limit is its capacity, though the mark is undefined, and each of its elements is initialized to zero. It will be having a backing array, and the array offset as zero.
public static ShortBuffer allocate(int capacity)
Parameters: The method accepts a mandatory parameter capacity which specifies the capacity of the new buffer, in shorts.
Return Value: This method returns the new ShortBuffer.
Exception: This method throws the IllegalArgumentException, if the capacity is a negative integer.
Below programs illustrate the use of allocate() method :
ShortBuffer: [10000, 10640, 10189, -2000, -16780]
Program 2: To show NullPointerException
Trying to allocate a negative integer Exception thrown: java.lang.IllegalArgumentException: capacity < 0: (-10 < 0)