LongBuffer allocate() method in Java
The allocate() method of java.nio.LongBuffer Class is used to allocate a new Long buffer next to the existing 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. It will have a backing array, and its array offset will be zero.
public static LongBuffer allocate(Long capacity)
Parameter: This method takes the new buffer’s capacity, in Long, as a parameter.
Return Value: This method returns the new Long buffer.
Exception: This method throws the IllegalArgumentException if the capacity is a negative Longer.
Below programs illustrate the allocate() method:
LongBuffer: [11, 0, 19, 0, 0, 0, 0, 0, 0, 0]
Program 2: To demonstrate IllegalArgumentException
Trying to allocate a Negative Longer 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.