ShortBuffer allocate() method in Java With Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()));
    }
}

chevron_right


Output:

ShortBuffer: [10000, 10640, 10189, -2000, -16780]

Program 2: To show NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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()



My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

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