LongBuffer allocate() method in Java Last Updated : 10 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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.Syntax: 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:Program 1: Java // Java program to demonstrate // allocate() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the LongBuffer int Capacity = 10; // Creating the LongBuffer // creating object of Longbuffer // and allocating size capacity LongBuffer ib = LongBuffer.allocate(Capacity); // putting the value in Longbuffer ib.put(11); ib.put(2, 19); System.out.println("LongBuffer: " + Arrays.toString(ib.array())); } } Output: LongBuffer: [11, 0, 19, 0, 0, 0, 0, 0, 0, 0] Program 2: To demonstrate IllegalArgumentException Java // Java program to demonstrate // allocate() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the LongBuffer // by negative Longer int Capacity = -10; // Creating the LongBuffer try { // creating object of Longbuffer // and allocating size with negative Longer System.out.println("Trying to allocate a Negative Longer"); LongBuffer ib = LongBuffer.allocate(Capacity); } catch (IllegalArgumentException e) { System.out.println("Exception thrown: " + e); } } } OutputTrying to allocate a Negative Longer Exception thrown: java.lang.IllegalArgumentException: capacity < 0: (-10 < 0) Comment More infoAdvertise with us Next Article LongBuffer allocate() method in Java P pawan_asipu Follow Improve Article Tags : Misc Java Java-Functions Java-NIO package Java-LongBuffer +1 More Practice Tags : JavaMisc Similar Reads LongBuffer duplicate() method in Java The duplicate() method of java.nio.LongBuffer Class is used to Create a new Long buffer that shares the given buffer's content. Syntax: public abstract LongBuffer duplicate() Return Value: This method returns the new Long buffer which is carrying the previous Long buffer content Below are the exampl 3 min read LongBuffer compact() method in Java The compact() method of java.nio.LongBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set 3 min read LongBuffer equals() method in Java The equals() method of java.nio.LongBuffer Class is used to check whether or not the given buffer is equal to another object. Two Long buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con 4 min read LongBuffer get() methods in Java get() The get() method of java.nio.LongBuffer Class is used to read the Long at the given buffer's current position, and then increment the position. Syntax : public abstract Long get() Return Value: This method returns the Long value at the buffer's current position. Throws: This method throws Buff 5 min read LongBuffer hasArray() method in Java The array() method of java.nio.LongBuffer Class is used to Return the Long array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensu 2 min read Like