ShortBuffer hasArray() method in Java with Examples Last Updated : 20 Sep, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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"); } } } Output: ShortBuffer sb is backed by array Program 2: When buffer is not backed by an array. Java // 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"); } } } Output: ShortBuffer sb is not backed by any array Comment More infoAdvertise with us Next Article ShortBuffer array() Method in Java with Examples I IshwarGupta Follow Improve Article Tags : Misc Java Java-Functions Java-ShortBuffer Java-NIO package +1 More Practice Tags : JavaMisc Similar Reads ShortBuffer array() Method in Java with Examples The array() method of java.nio.ShortBuffer is used to return the short array that backs this buffer(optional). Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke the hasArray method before invoking this method in order to ensure that 3 min read ShortBuffer equals() method in Java with Examples The equals() method of java.nio.ShortBuffer Class is used to check whether or not the given buffer is equal to another object. Two short buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, c 3 min read Buffer hasArray() methods in Java with Examples The hasArray() method of java.nio.Buffer class is used to tell whether or not this buffer is backed by an accessible array. If this method returns true then the array and arrayOffset() methods may safely be invoked. Syntax: public abstract boolean hasArray() Returns: This method will return true if, 2 min read ShortBuffer arrayOffset() method in Java With Examples The arrayOffset() method of java.nio.ShortBuffer class is used to return the offset within the bufferâs backing array of the first element of the buffer. It means that if this buffer is backed by an array, then buffer position p corresponds to array index p + arrayOffset(). In-order to check whether 3 min read ByteBuffer hasArray() method in Java with Examples The hasArray() method of java.nio.ByteBuffer class is used to ensure whether or not the given buffer is backed by an accessible byte 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() m 2 min read FloatBuffer hasArray() method in Java with Examples The hasArray() method of java.nio.FloatBuffer 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() 2 min read Like