Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaShortBuffer hasArray() method in Java with Examples

ShortBuffer hasArray() method in Java with Examples

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


Output:

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


Output:

ShortBuffer sb is not backed by any array
RELATED ARTICLES

Most Popular

Recent Comments