The java.nio.IntBuffer
is a class that is used to store a buffer of integers. The hasArray()
method of the class java.nio.IntBuffer
is used to check if a buffer has an accessible integer array backing it. An accessible array is an array that is not read-only.
The IntBuffer.hasArray()
method is declared as follows:
buff.hasArray()
buff
: The IntBuffer
to be checked to see if it has an accessible integer array backing it.The IntBuffer.hasArray()
method returns a boolean
such that:
true
if buff
has an accessible integer array backing it.false
if buff
does not have an accessible integer array backing it.This means that IntBuffer.hasArray()
returns true
only if buff
has an integer array backing it and that array is not read-only.
Consider the code snippet below, which demonstrates the use of the IntBuffer.hasArray()
method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {IntBuffer buff1 = IntBuffer.allocate(n1);buff1.put(1);buff1.put(4);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");}}}
IntBuffer
buff1
is declared in line 7 with capacity n1 = 5
.buff1
using the put()
method in lines 8-9.IntBuffer.hasArray()
method is used in line 12 to check if buff1
has an accessible integer array backing it. The IntBuffer.hasArray()
method returns true
.Consider another example of the IntBuffer.hasArray()
method:
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 5;try {IntBuffer buff1 = IntBuffer.allocate(n1);buff1.put(1);buff1.put(4);System.out.println("buff1: " + Arrays.toString(buff1.array()));IntBuffer 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");}}}
IntBuffer
buff1
is declared in line 7 with capacity n1 = 5
.buff1
using the put()
method in lines 8-9.IntBuffer
buff2
is declared in line 12 that is the read-only copy of buff1
.IntBuffer.hasArray()
method is used in line 14 to check if buff1
has an accessible integer array backing it. The IntBuffer.hasArray()
method returns true
.IntBuffer.hasArray()
method is used in line 16 to check if buff2
has an accessible integer array backing it. The IntBuffer.hasArray()
method returns false
. This is because buff2
is read-only.Free Resources