What is the IntBuffer hasArray() method in Java?

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.

Declaration

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.

Return value

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

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

Examples

Example 1

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");
}
}
}

Explanation

  • An IntBuffer 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 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.

Example 2

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");
}
}
}

Explanation

  • An IntBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in lines 8-9.
  • An IntBuffer buff2 is declared in line 12 that is the read-only copy of buff1.
  • The 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.
  • The 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.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved