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)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.