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, and its array offset will be zero.
Syntax:
public static CharBuffer allocate(int capacity)
Parameter: This method takes the new buffer’s capacity, in char, as a parameter.
Return Value: This method returns the new char buffer.
Exception: This method throws the IllegalArgumentException if the capacity is a negative integer.
Below program illustrates the allocate() method:
Examples 1:
// 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 CharBuffer char capacity = 10; // Creating the CharBuffer // creating object of Charbuffer // and allocating size capacity CharBuffer fb = CharBuffer.allocate(capacity); // putting the value in charbuffer fb.put('a'); fb.put(3, 'b'); // Printing the CharBuffer System.out.println("ChartBuffer: " + Arrays.toString(fb.array())); } } |
ChartBuffer: [a, , , b, , , , , , ]
Examples 2: To demonstrate IllegalArgumentException
// 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 CharBuffer // by negetive integer int capacity = -10; // Creating the CharBuffer try { // creating object of CharBuffer // and allocating size with negative intger System.out.println("Trying to allocate a negative integer"); CharBuffer fb = CharBuffer.allocate(capacity); } catch (IllegalArgumentException e) { System.out.println("Exception thrown: " + e); } } } |
Trying to allocate a negative integer Exception thrown: java.lang.IllegalArgumentException
Recommended Posts:
- DoubleBuffer allocate() method in Java With Examples
- FloatBuffer allocate() method in Java With Examples
- ByteBuffer allocate() method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- CharsetEncoder encode(CharBuffer in) method in Java with Examples
- CharArrayReader read(CharBuffer) method in Java with Examples
- StringReader read(CharBuffer) method in Java with Examples
- Reader read(CharBuffer) method in Java with Examples
- CharBuffer mark() methods in Java with Examples
- CharBuffer order() methods in Java with Examples
- CharBuffer read() methods in Java with Examples
- CharBuffer limit() methods in Java with Examples
- CharBuffer flip() methods in Java with Examples
- CharBuffer position() methods in Java with Examples
- CharBuffer length() methods in Java with Examples
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.



