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.
Syntax:
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 :
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 ShortBuffer int capacity = 5; // Creating the ShortBuffer // creating object of Shortbuffer // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity); // putting the value in Shortbuffer sb.put((short)10000); sb.put((short)10640); sb.put((short)10189); sb.put((short)-2000); sb.put((short)-16780); // Printing the ShortBuffer System.out.println("ShortBuffer: " + Arrays.toString(sb.array())); } } |
ShortBuffer: [10000, 10640, 10189, -2000, -16780]
Program 2: To show NullPointerException
// 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 ShortBuffer // by negetive integer int capacity = -10; // Creating the ShortBuffer try { // creating object of shortbuffer // and allocating size with negative 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
Reference: https://docs.oracle.com/javase/7/docs/api/java/nio/ShortBuffer.html#allocate()
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ShortBuffer asReadOnlyBuffer() method in Java with Examples
- ShortBuffer compact() method in Java With Examples
- ShortBuffer arrayOffset() method in Java With Examples
- ShortBuffer mark() method in Java with Examples
- ShortBuffer toString() method in Java with Examples
- ShortBuffer rewind() method in Java with Examples
- ShortBuffer order() Method in Java with Examples
- ShortBuffer reset() method in Java with Examples
- ShortBuffer limit() method in Java with Examples
- ShortBuffer slice() method in Java with Examples
- ShortBuffer hasArray() method in Java with Examples
- ShortBuffer isDirect() Method in Java with Examples
- ShortBuffer equals() method in Java with Examples
- ShortBuffer duplicate() method in Java With Examples
- ShortBuffer hashCode() Method in Java with Examples
- ShortBuffer array() Method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- ShortBuffer compareTo method in Java with Examples
- DoubleBuffer allocate() method in Java With Examples
- CharBuffer 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

