What is the floatBuffer hasArray() method in Java?
The java.nio.FloatBuffer is a class we can use to store a buffer of floats. The hasArray() method of this class checks if a buffer has an accessible
Declaration
The FloatBuffer.hasArray() method is declared as follows:
buff.hasArray()
buff: TheFloatBufferwe will check to determine whether it has an accessible float array backing it.
Return value
The FloatBuffer.hasArray() method returns a boolean such that:
- The return value is
trueifbuffhas an accessible float array backing it. - The return value is
falseifbuffdoes not have an accessible float array backing it.
This means that FloatBuffer.hasArray() returns true only if buff has a float array backing it and that array is not read-only.
Code
Example 1
Consider the code snippet below, which demonstrates the FloatBuffer.hasArray() method when a backing array is present:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {FloatBuffer buff1 = FloatBuffer.allocate(n1);buff1.put(1.5F);buff1.put(4.6F);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
FloatBufferbuff1is declared in line 7 with capacityn1 = 5. - Two elements are added to
buff1using theput()method in line 8-9. - The
FloatBuffer.hasArray()method is used in line 12 to check ifbuff1has an accessible float array backing it. TheFloatBuffer.hasArray()method returnstrue.
Example 2
Consider another example of the FloatBuffer.hasArray() method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {FloatBuffer buff1 = FloatBuffer.allocate(n1);buff1.put(1.5F);buff1.put(4.6F);System.out.println("buff1: " + Arrays.toString(buff1.array()));FloatBuffer 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
FloatBufferbuff1is declared in line 7 with capacityn1 = 5. - Two elements are added to
buff1using theput()method in line 8-9. - A
FloatBufferbuff2is declared in line 12 which is the read-only copy ofbuff1. - The
FloatBuffer.hasArray()method is used in line 14 to check ifbuff1has an accessible float array backing it. TheFloatBuffer.hasArray()method returnstrue. - The
FloatBuffer.hasArray()method is used in line 16 to check ifbuff2has an accessible float array backing it. TheFloatBuffer.hasArray()method returnsfalse. This is becausebuff2is read-only.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved