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
The FloatBuffer.hasArray()
method is declared as follows:
buff.hasArray()
buff
: The FloatBuffer
we will check to determine whether it has an accessible float array backing it.The FloatBuffer.hasArray()
method returns a boolean
such that:
true
if buff
has an accessible float array backing it.false
if buff
does 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.
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");}}}
FloatBuffer
buff1
is declared in line 7 with capacity n1 = 5
.buff1
using the put()
method in line 8-9.FloatBuffer.hasArray()
method is used in line 12 to check if buff1
has an accessible float array backing it. The FloatBuffer.hasArray()
method returns true
.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");}}}
FloatBuffer
buff1
is declared in line 7 with capacity n1 = 5
.buff1
using the put()
method in line 8-9.FloatBuffer
buff2
is declared in line 12 which is the read-only copy of buff1
.FloatBuffer.hasArray()
method is used in line 14 to check if buff1
has an accessible float array backing it. The FloatBuffer.hasArray()
method returns true
.FloatBuffer.hasArray()
method is used in line 16 to check if buff2
has an accessible float array backing it. The FloatBuffer.hasArray()
method returns false
. This is because buff2
is read-only.