Open In App

Buffer array() methods in Java with Examples

Last Updated : 28 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The array() method of java.nio.Buffer class is used to return the array that backs the taken buffer. This method is intended to allow array-backed buffers to be passed to native code more efficiently. Concrete subclasses provide more strongly-typed return values for this method. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke the hasArray method before invoking this method in order to ensure that this buffer has an accessible backing array. Syntax:
public abstract Object array()
Return Value: This method returns the array that backs this buffer. Exception: This method throws the ReadOnlyBufferException, If this buffer is backed by an array but is read-only. Below are the examples to illustrate the array() method: Example 1:
Output:
array is : [ 20 30 40 50 ]
Example 2:
Output:
buffer is backed by an array but is read-only
Exception throws: java.nio.ReadOnlyBufferException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#array--

Next Article
Practice Tags :

Similar Reads