Horje
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:
 

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




Reffered: https://www.geeksforgeeks.org


Java

Related
IntBuffer duplicate() method in Java with Examples IntBuffer duplicate() method in Java with Examples
ShortBuffer compact() method in Java With Examples ShortBuffer compact() method in Java With Examples
LongBuffer equals() method in Java LongBuffer equals() method in Java
IntBuffer array() method in Java IntBuffer array() method in Java
IntBuffer compact() method in Java IntBuffer compact() method in Java

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
7