What is the DoubleBuffer hasArray() method in Java?
The java.nio.DoubleBuffer is a class that is used to store a buffer of doubles. The hasArray() method of the class java.nio.DoubleBuffer is used to check if a buffer has an accessible double array backing it. An accessible array is an array that is not read-only.
Declaration
The DoubleBuffer.hasArray() method is declared as follows:
buff.hasArray()
buff: TheDoubleBufferto be checked if it has an accessible double array backing it.
Return value
The DoubleBuffer.hasArray() method returns a boolean such that:
- The return value is
trueifbuffhas an accessible double array backing it. - The return value is
falseifbuffdoes not have an accessible double array backing it.
This means that DoubleBuffer.hasArray() returns true only if buff has a double array backing it and that array is not read-only.
Examples
Example 1
Consider the code snippet below, which demonstrates the use of the DoubleBuffer.hasArray() method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {DoubleBuffer buff1 = DoubleBuffer.allocate(n1);buff1.put(1.5);buff1.put(4.6);System.out.println("buff1: " + Arrays.toString(buff1.array()));boolean foo = buff1.hasArray();System.out.println("\nbuff1 has an accessible array backing it: " + foo);} catch (IllegalArgumentException e) {System.out.println("Error!!! IllegalArgumentException");} catch (ReadOnlyBufferException e) {System.out.println("Error!!! ReadOnlyBufferException");}}}
Explanation
- A
DoubleBufferbuff1is declared in line 7 with capacityn1 = 5. - Two elements are added to
buff1using theput()method in lines 8-9. - The
DoubleBuffer.hasArray()method is used in line 12 to check ifbuff1has an accessible double array backing it. TheDoubleBuffer.hasArray()method returnstrue.
Example 2
Consider another example of the DoubleBuffer.hasArray() method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {DoubleBuffer buff1 = DoubleBuffer.allocate(n1);buff1.put(1.5);buff1.put(4.6);System.out.println("buff1: " + Arrays.toString(buff1.array()));DoubleBuffer buff2 = buff1.asReadOnlyBuffer();boolean foo1 = buff1.hasArray();System.out.println("\nbuff1 has an accessible array backing it: " + foo1);boolean foo2 = buff2.hasArray();System.out.println("buff2 has an accessible array backing it: " + foo2);} catch (IllegalArgumentException e) {System.out.println("Error!!! IllegalArgumentException");} catch (ReadOnlyBufferException e) {System.out.println("Error!!! ReadOnlyBufferException");}}}
Explanation
- A
DoubleBufferbuff1is declared in line 7 with capacityn1 = 5. - Two elements are added to
buff1using theput()method in lines 8-9. - A
DoubleBufferbuff2is declared in line 12 that is the read-only copy ofbuff1. - The
DoubleBuffer.hasArray()method is used in line 14 to check ifbuff1has an accessible double array backing it. TheDoubleBuffer.hasArray()method returnstrue. - The
DoubleBuffer.hasArray()method is used in line 16 to check ifbuff2has an accessible double array backing it. TheDoubleBuffer.hasArray()method returnsfalse. This is becausebuff2is read-only.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved