CharBuffer hasArray() method in JavaLast Updated : 27 Mar 2025 The java.nio.CharBuffer contains the hasArray() function. The CharBuffer class is used to check if an accessible char array backs up the buffer that is provided. If this buffer has an accessible backing array, it returns true; otherwise, it returns false. The array() and arrayOffset() methods can be securely called since they operate on the backing array if this method returns true. Syntax: Return Value: If and only if this buffer is not read-only and is supported by an array, then this function will return true. If not, false is returned. Example 1:The code illustrates how to use Java's CharBuffer. The characters 'a' and 'b' are inserted at predetermined points in a CharBuffer that has a capacity of 12 characters. It uses the hasArray() method to determine whether the buffer is supported by an array after rewinding it to the beginning. Although they are exceptional in this situation, the code manages possible problems like ReadOnlyBufferException and IllegalArgumentException. The CharBuffer's storage array backing is verified by the hasArray() method, and the result is reported. Implementation:FileName: CharBufferExample1 .java Output: CharBuffer chabuff is backed by the array Example 2:The code illustrates how to create and use a CharBuffer in Java. It starts by initializing a 12-character CharBuffer and fills it with the characters "a" and "b" at certain locations. It uses asReadOnlyBuffer() to make a read-only copy of the buffer after it has been rewound. The hasArray() function prints the result after determining whether this read-only buffer is supported by an array. The method also manages errors such as ReadOnlyBufferException and IllegalArgumentException, although the latter is rare in this case because buffer operations are expected. Implementation:FileName: CharBufferExample2.java Output: CharBuffer charbuff is not backed by the any array |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India