Skip to content
Related Articles

Related Articles

Improve Article

ShortBuffer allocate() method in Java With Examples

  • Last Updated : 27 May, 2021
Geek Week

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




// 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()));
    }
}
Output: 
ShortBuffer: [10000, 10640, 10189, -2000, -16780]

 

Program 2: To show NullPointerException
 

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 ShortBuffer
        // by negative 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 negative integer");
 
            FloatBuffer fb = FloatBuffer.allocate(capacity);
        }
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}
Output
Trying to allocate a negative integer
Exception thrown: java.lang.IllegalArgumentException: capacity < 0: (-10 < 0)

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 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :