The hasArray() method of the Java CharBuffer class checks whether a buffer has an accessible backing character array. It returns true if the buffer has an accessible backing array and false otherwise.
In this chapter, you will learn about the hasArray() method of the CharBuffer class, its syntax, return value, and examples demonstrating its behavior with writable and read-only buffers.
The hasArray() method belongs to the java.nio.CharBuffer class. It determines whether the buffer is backed by an accessible character array.
If the method returns true, the backing array can be accessed using the array() and arrayOffset() methods. If the method returns false, the buffer does not provide access to a backing array.
A read-only CharBuffer returns false from hasArray(), even if its underlying storage is backed by an array.
The following syntax demonstrates the declaration of the hasArray() method.
public final boolean hasArray()
The hasArray() method returns a boolean value.
The following examples demonstrate how the hasArray() method behaves with a writable CharBuffer and a read-only CharBuffer.
In the following example, we create a CharBuffer with a capacity of 12 characters, insert characters at specific positions, and use the hasArray() method to check whether the buffer has an accessible backing array.
Output:
CharBuffer chabuff is backed by the array
In the following example, we create a writable CharBuffer, insert characters, and create a read-only view using asReadOnlyBuffer(). We then call the hasArray() method to check whether the read-only buffer provides access to its backing array.
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