DoubleBuffer arrayOffset() Method in Java With ExamplesLast Updated : 25 Mar 2025 The offset of the buffer's first element within its underlying array can be obtained by calling the arrayOffset() method of the java.nio.DoubleBuffer class. In other words, buffer position p corresponds to array index p + array Offset() if this buffer is supported by an array. We can use the hasArray() method to see if this buffer has a backing array. The backing array of this buffer has been ensured to be accessible by it. Syntax: Return Value: The offset of the buffer's first entry within its array is returned by this method. Exception: If this buffer has an array behind it but is read-only, this function throws a ReadOnlyBufferException. Example 1:The code creates a DoubleBuffer with the name floatbuff and a capacity of 10. Arrays.toString(floatbuff.array()), which shows the buffer's underlying array, is used to print the contents of the buffer. To display the offset within the array at which the buffer's contents begin, the arrayOffset() method is invoked. The offset denotes the location of the buffer's data within the underlying array. Read-only buffer operations and exception handling for invalid arguments are included in the source. Implementation:FileName: BufferArrayExample1.java Output: The DoubleBuffer is given by : [9.630000114440918, 0.0, 0.0, 0.0, 7.360000133514404, 0.0, 0.0, 0.0, 0.0, 0.0] The arrayOffset is given by : 0 Example 2:The code first creates a DoubleBuffer with a capacity of 10, adds values, and then uses asReadOnlyBuffer() to generate a read-only duplicate of the buffer. The contents of the buffer are visible in the read-only buffer (floatbuff1) that is presented. It creates a ReadOnlyBufferException if we try to display this read-only buffer's array offset using arrayOffset(). The reason for this problem is that read-only buffers do not provide the underlying array information, so arrayOffset() is not supported on them. The code handles read-only buffer operations and illegal argument problems with exceptions. Implementation:FileName: BufferArrayExample2.java Output: The Read only buffer is given by : 9.630000114440918, 0.0, 0.0, 0.0, 8.630000114440918, 0.0, 0.0, 0.0, 0.0, 0.0, Try to print the offset of the array. of read only buffer The Exception thrown is: java.nio.ReadOnlyBufferException |
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