ByteBuffer allocate() method in Java with Examples Last Updated : 25 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The allocate() method of java.nio.ByteBuffer class is used to allocate a new byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. It will have a backing array, and its array offset will be zero. Syntax : public static ByteBuffer allocate(int capacity) Parameters: This method takes capacity, in bytes as parameter. Return Value: This method returns the new byte buffer . Throws: This method throws IllegalArgumentException, If the capacity is a negative integer Below are the examples to illustrate the allocate() method: Examples 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 ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the int to byte typecast value // in ByteBuffer using putInt() method bb.put((byte)20); bb.put((byte)30); bb.put((byte)40); bb.put((byte)50); bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: " + Arrays.toString(bb.array())); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } Output: Original ByteBuffer: [20, 30, 40, 50] Examples 2: 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 with negative // value of the ByteBuffer int capacity = -4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity System.out.println("Trying to allocate negative"+ " value in ByteBuffer"); ByteBuffer bb = ByteBuffer.allocate(capacity); // putting int to byte typecast value // in ByteBuffer using putInt() method bb.put((byte)20); bb.put((byte)30); bb.put((byte)40); bb.put((byte)50); bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: " + Arrays.toString(bb.array())); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown : " + e); } } } Output: Trying to allocate negative value in ByteBuffer Exception thrown : java.lang.IllegalArgumentException Comment More infoAdvertise with us Next Article ByteBuffer allocate() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Misc Java Java-Functions Java-NIO package Java-ByteBuffer +1 More Practice Tags : JavaMisc Similar Reads ByteBuffer allocateDirect() method in Java with Examples The allocateDirect() method of java.nio.ByteBuffer class is used to Allocate a new direct byte buffer. The new buffer's position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. Whether or not it has a backing array is un 2 min read DoubleBuffer allocate() method in Java With Examples The allocate() method of java.nio.DoubleBuffer Class is used to allocate a new double buffer next to the existing buffer. The new bufferâs position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing a 2 min read CharBuffer allocate() method in Java with Examples The allocate() method of java.nio.CharBuffer Class is used to allocate a new char buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing array 2 min read ByteBuffer asFloatBuffer() method in Java with Examples The asFloatBuffer() method of java.nio.ByteBuffer class is used to create view of this byte buffer as a float buffer.The content of the new buffer will start from this buffer's current position. Changes made to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' 3 min read ByteBuffer array() method in Java with Examples The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that 3 min read Like