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 float arrayarray for storing underlying values backing it. An accessible array is an array that is not read-only.

Declaration

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.

Return value

The FloatBuffer.hasArray() method returns a boolean such that:

  • The return value is true if buff has an accessible float array backing it.
  • The return value is 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.

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 FloatBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in line 8-9.
  • The 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.

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 FloatBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in line 8-9.
  • A FloatBuffer buff2 is declared in line 12 which is the read-only copy of buff1.
  • The 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.
  • The 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.
Copyright ©2024 Educative, Inc. All rights reserved