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.
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:
ChartBuffer: [a, , , b, , , , , , ]
Examples 2: To demonstrate IllegalArgumentException
Trying to allocate a negative integer Exception thrown: java.lang.IllegalArgumentException
- ByteBuffer allocate() method in Java with Examples
- DoubleBuffer allocate() method in Java With Examples
- FloatBuffer allocate() method in Java With Examples
- ShortBuffer allocate() method in Java With Examples
- Reader read(CharBuffer) method in Java with Examples
- CharArrayReader read(CharBuffer) method in Java with Examples
- CharsetEncoder encode(CharBuffer in) method in Java with Examples
- StringReader read(CharBuffer) method in Java with Examples
- LongBuffer allocate() method in Java
- IntBuffer allocate() method in Java
- CharBuffer equals() method in Java
- CharBuffer duplicate() method in Java
- CharBuffer array() method in Java
- CharBuffer slice() method in Java
- CharBuffer compareTo() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.