IntBuffer.allocate(int): This method is available in java.nio.IntBuffer class of Java.
Syntax:
IntBuffer java.nio.IntBuffer.allocate(int capacity)
This method takes one argument of type int as its parameter. This method allocates a new int buffer.
Note: The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, each of its elements will be initialized to zero, and its byte order will be the native order of the underlying hardware.
Parameters: One parameter is required for this method.
capacity: The new buffer's capacity, in ints.
Returns: The new int buffer.
Throws:
IllegalArgumentException - If the capacity is a negative integer
Approach 1: When no exceptions
Java
import java.nio.IntBuffer;public class IntBufferallocate {public static void main(String[] args) {int capacity = 100;System.out.println(IntBuffer.allocate(capacity));}}
Output:
java.nio.HeapIntBuffer[pos=0 lim=100 cap=100]
Approach 2: IllegalArgumentException
Java
import java.nio.IntBuffer;public class IntBufferallocate {public static void main(String[] args) {int capacity = -100;System.out.println(IntBuffer.allocate(capacity));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: capacity < 0: (-100 < 0) at java.base/java.nio.Buffer.createCapacityException(Buffer.java:275) at java.base/java.nio.IntBuffer.allocate(IntBuffer.java:360)
No comments:
Post a Comment