FloatBuffer allocate() method in Java With Examples
The allocate() method of java.nio.FloatBuffer Class is used to allocate a new float 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 FloatBuffer allocate(int capacity)
Parameter: This method takes the new buffer’s capacity, in float, as a parameter.
Return Value: This method returns the new float buffer.
Exception: This method throws the IllegalArgumentException if the capacity is a negative integer.
Below program illustrates the allocate() method:
Examples 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 FloatBuffer int capacity = 10 ; // Creating the FloatBuffer // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer fb.put( 8 .56F); fb.put( 2 , 9 .61F); System.out.println( "FloatBuffer: " + Arrays.toString(fb.array())); } } |
FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Examples 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 FloatBuffer // by negetive integer int capacity = - 10 ; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size with negetive integer System.out.println( "Trying to allocate a negetive integer" ); FloatBuffer fb = FloatBuffer.allocate(capacity); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown: " + e); } } } |
Trying to allocate a negetive integer Exception thrown: java.lang.IllegalArgumentException
Recommended Posts:
- FloatBuffer arrayOffset() method in Java With Examples
- FloatBuffer hasArray() method in Java with Examples
- FloatBuffer compareTo() method in Java With Examples
- FloatBuffer limit() Method in Java with Examples
- FloatBuffer slice() method in Java with Examples
- FloatBuffer wrap() method in Java with Examples
- FloatBuffer asReadOnlyBuffer() method in Java with Examples
- FloatBuffer array() method in Java With Examples
- FloatBuffer duplicate() method in Java with Examples
- FloatBuffer compact() method in Java With Examples
- FloatBuffer equals() method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- DoubleBuffer allocate() method in Java With Examples
- CharBuffer allocate() method in Java with Examples
- ByteBuffer allocate() method in Java with Examples
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.
Improved By : nidhi_biet