The hasArray() method of java.nio.ShortBuffer class is used to ensure whether or not the given buffer is backed by an accessible float array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() methods may safely be invoked, as they work on the backing array.
Syntax:
public final boolean hasArray()
Returns: This method will return true if, and only if, this buffer is backed by an array and is not read-only. Else it returns false.
Below are the examples to illustrate the hasArray() method:
Program 1:
When buffer is backed by an array.
// Java program to demonstrate // hasArray() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ShortBuffer int capacity = 10; // Creating the ShortBuffer try { // creating object of Shortbuffer // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity); // putting the value in Shortbuffer sb.put((short)256); sb.put(2, (short)91); sb.rewind(); // checking ShortBuffer sb is backed by array or not boolean isArray = sb.hasArray(); // checking if else condition if (isArray) System.out.println("ShortBuffer sb is" + " backed by array"); else System.out.println("ShortBuffer sb is" + " not backed by any array"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
ShortBuffer sb is backed by array
Program 2:
When buffer is not backed by an array.
// Java program to demonstrate // hasArray() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ShortBuffer int capacity = 10; // Creating the ShortBuffer try { // creating object of Shortbuffer // and allocating size capacity ShortBuffer sb = ShortBuffer.allocate(capacity); // putting the value in Shortbuffer sb.put((short)856); sb.put(2, (short)961); sb.rewind(); // Creating a read-only copy of ShortBuffer // using asReadOnlyBuffer() method ShortBuffer sb1 = sb.asReadOnlyBuffer(); // checking ShortBuffer sb is backed by array or not boolean isArray = sb1.hasArray(); // checking if else condition if (isArray) System.out.println("ShortBuffer sb is" + " backed by array"); else System.out.println("ShortBuffer sb is" + " not backed by any array"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
ShortBuffer sb is not backed by any array
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ByteBuffer hasArray() method in Java with Examples
- DoubleBuffer hasArray() method in Java with Examples
- FloatBuffer hasArray() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- ShortBuffer asReadOnlyBuffer() method in Java with Examples
- ShortBuffer arrayOffset() method in Java With Examples
- ShortBuffer equals() method in Java with Examples
- ShortBuffer order() Method in Java with Examples
- ShortBuffer compact() method in Java With Examples
- ShortBuffer array() Method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- ShortBuffer duplicate() method in Java With Examples
- ShortBuffer hashCode() Method in Java with Examples
- ShortBuffer slice() method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- ShortBuffer mark() method in Java with Examples
- ShortBuffer rewind() method in Java with Examples
- ShortBuffer limit() method in Java with Examples
- ShortBuffer isDirect() Method in Java with Examples
- ShortBuffer toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

