CharBuffer array() method in Java Last Updated : 19 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer's content will cause the returned array's content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to ensure that this buffer has an accessible backing array. Syntax: public final char[] array() Return Value: This method returns the array that backs this buffer. Throws: This method throws the ReadOnlyBufferException(If this buffer is backed by an array but is read-only) Below program illustrates the array() method: Example 1: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the CharBuffer int capacity = 10; // Creating the CharBuffer try { // creating object of CharBuffer // and allocating size capacity CharBuffer cb = CharBuffer.allocate(capacity); // putting the value in CharBuffer cb.put('a'); cb.put(2, 'b'); cb.rewind(); // getting array from cb // CharBuffer using array() method char[] cbb = cb.array(); // printing the CharBuffer cb System.out.println("CharBuffer: " + Arrays.toString(cbb)); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } Output: CharBuffer: [a, , b, , , , , , , ] Example 2: Java // Java program to demonstrate // array() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the cb int capacity1 = 10; // Declaring the capacity of the cb1 int capacity2 = 5; // Creating the CharBuffer try { // // cb // // creating object of CharBuffer cb // and allocating size capacity CharBuffer cb = CharBuffer.allocate(capacity1); // putting the value in cb cb.put('a'); cb.put(2, 'b'); cb.put(3, 'c'); cb.rewind(); // print the CharBuffer System.out.println("CharBuffer cb: " + Arrays.toString(cb.array())); // // cb1 // // creating object of CharBuffer cb1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity2); // putting the value in cb1 cb1.put(1, 'd'); cb1.put(2, 'e'); cb1.rewind(); // print the CharBuffer System.out.println("\nCharBuffer cb1: " + Arrays.toString(cb1.array())); // Creating a read-only copy of CharBuffer // using asReadOnlyBuffer() method CharBuffer readOnlycb = cb.asReadOnlyBuffer(); // print the CharBuffer System.out.print("\nReadOnlyBuffer CharBuffer: "); while (readOnlycb.hasRemaining()) System.out.print(readOnlycb.get() + ", "); // try to change readOnlycb System.out.println("\n\nTrying to get the array" + " from ReadOnlycb for editing"); char[] cbarr = readOnlycb.array(); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown: " + e); } } } Output: CharBuffer cb: [a,, b, c,,,,,, ] CharBuffer cb1: [, d, e,, ] ReadOnlyBuffer CharBuffer: a,, b, c,,,,,,, Trying to get the array from ReadOnlycb for editing Exception thrown: java.nio.ReadOnlyBufferException Comment More infoAdvertise with us Next Article CharBuffer array() method in Java gopaldave Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-CharBuffer Java-NIO package +2 More Practice Tags : JavaMisc Similar Reads CharBuffer arrayOffset() method in Java The arrayOffset() method of java.nio.CharBuffer class is used to return the offset within the buffer's backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). Inorder to check whether t 3 min read Array getChar() Method in Java The java.lang.reflect.Array.getChar() is an inbuilt method in Java and is used to return the element present at a given index from the specified Array as a char. Syntax Array.getChar(Object []array,int index) Parameters : array : The object array whose index is to be returned.index : The particular 3 min read CharBuffer wrap() method in Java wrap(char[ ] array) The wrap() method of java.nio.CharBuffer Class is used to wrap a character array into a buffer. The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity i 4 min read CharBuffer get() methods in Java The get() method of java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position. Syntax: public abstract char get() Return Value: This method returns the char value at the buffer's current position. Exceptions: This method throws Buffe 5 min read CharBuffer put() methods in Java put(char i) The put(char i) method of java.nio.CharBuffer Class is used to write the given character into the newly created char buffer at the current position and then increments the position. Syntax : public abstract CharBuffer put(char i) Parameters: This method takes the char value i as a parame 6 min read CharBuffer asReadOnlyBuffer() method in Java The asReadOnlyBuffer() method of java.nio.CharBuffer Class is used to create a new, read-only char buffer with this buffer's content. The new buffer is a replica of this buffer. Hence changes made to this buffer's content will be visible in the new buffer. Since the new buffer is read-only, therefor 3 min read CharBuffer slice() method in Java slice() method of java.nio.charBuffer Class is used to create a new char buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start from this buffer's current position. The new buffer will show the changes made in the buffer's content, and vi 3 min read IntBuffer array() method in Java The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure 3 min read CharBuffer equals() method in Java The equals() method of java.nio.CharBuffer Class is used to check whether or not the given buffer is equal to another object. Two char buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con 4 min read CharBuffer compact() method in Java The compact() method of java.nio.CharBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set 3 min read Like