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: The DoubleBuffer to 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 true if buff has an accessible double array backing it.
  • The return value is false if buff does 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 DoubleBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in lines 8-9.
  • The DoubleBuffer.hasArray() method is used in line 12 to check if buff1 has an accessible double array backing it. The DoubleBuffer.hasArray() method returns true.

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 DoubleBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in lines 8-9.
  • A DoubleBuffer buff2 is declared in line 12 that is the read-only copy of buff1.
  • The DoubleBuffer.hasArray() method is used in line 14 to check if buff1 has an accessible double array backing it. The DoubleBuffer.hasArray() method returns true.
  • The DoubleBuffer.hasArray() method is used in line 16 to check if buff2 has an accessible double array backing it. The DoubleBuffer.hasArray() method returns false. This is because buff2 is read-only.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved