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.
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 Longeger.
Below programs illustrate the allocate() method:
Program 1:
// 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())); } } |
LongBuffer: [11, 0, 19, 0, 0, 0, 0, 0, 0, 0]
Program 2: To demonstrate IllegalArgumentException
// 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 Longeger int Capacity = - 10 ; // Creating the LongBuffer try { // creating object of Longbuffer // and allocating size with negative Longger System.out.println( "Trying to allocate a Negative Longeger" ); LongBuffer ib = LongBuffer.allocate(Capacity); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown: " + e); } } } |
Trying to allocate a Negative Longeger Exception thrown: java.lang.IllegalArgumentException
Recommended Posts:
- LongBuffer compact() method in Java
- LongBuffer asReadOnlyBuffer() method in Java
- LongBuffer equals() method in Java
- LongBuffer slice() method in Java
- LongBuffer duplicate() method in Java
- LongBuffer hasArray() method in Java
- LongBuffer wrap() method in Java
- LongBuffer arrayOffset() method in Java
- LongBuffer mark() method in Java with Examples
- LongBuffer limit() method in Java with Examples
- LongBuffer flip() method in Java with Examples
- LongBuffer reset() method in Java with Examples
- LongBuffer rewind() method in Java with Examples
- LongBuffer clear() method in Java with Examples
- IntBuffer allocate() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.