What is the ByteBuffer hasArray() method in Java?

What is the ByteBuffer hasArray() method in Java? Answer

In Java, the hasArray() method of the ByteBuffer class confirms whether or not a ByteBuffer object has an accessible byte array.

The process is illustrated below:

To use the hasArray() method, you will need to import the ByteBuffer class into your program, as shown below:

import java.nio.ByteBuffer

The prototype of the hasArray() method is shown below:

public final boolean hasArray()

Parameters

The hasArray() method does not accept any parameters.

Return value

If the buffer has an accessible byte array and is not set to read-only, then the hasArray() method returns true. Otherwise, it returns false.

Note: If the hasArray() method returns true, then you can safely invoke the array() and arrayOffset() methods.

Example

The code below shows how the hasArray() method can be used in Java:

import java.nio.*;
import java.util.*;

class hasArrayMethod {
  public static void main(String[] args) {

    // initialize ByteBuffer instance
    ByteBuffer bufferOne = ByteBuffer.allocate(5);

    byte values[] = {2, 1, 6, 7, 10};

    // add values to buffer
    for(int i = 0; i < 5; i++)
    {
      bufferOne.put(values[i]);
    }

    // create a new read-only buffer
    ByteBuffer bufferTwo = bufferOne.asReadOnlyBuffer();

    // check if bufferOne has a backing array
    if(bufferOne.hasArray())
    {
      System.out.println("BufferOne has an accessible byte array.");
    }
    else
    {
      System.out.println("BufferOne does not have an accessible byte array.");
    }

    // check if bufferTwo has a backing array
    if(bufferTwo.hasArray())
    {
      System.out.println("BufferTwo has an accessible byte array.");
    }
    else
    {
      System.out.println("BufferTwo does not have an accessible byte array.");
    }

  }
}

Explanation

First, a ByteBuffer object called bufferOne is initialized through the allocate() method. bufferOne has the capacity to store five elements.

Next, a for loop adds values to bufferOne through the put() method.

A second ByteBuffer object called bufferTwo is initialized in line 1919. Since bufferTwo is created through the asReadOnlyBuffer() method, it has the same values as bufferOne, but its read-only attribute is set as true.

The hasArray() method in line 2222 checks if bufferOne has an accessible byte array. Since bufferOne does have a valid byte array and is not set to read-only, the hasArray() method returns true.

Finally, the hasArray() method in line 3232 checks if bufferTwo has an accessible byte array. Since bufferTwo is set to read-only, the hasArray() method returns false.

What is the ByteBuffer hasArray() method in Java? Review:

In our experience, we suggest you solve this What is the ByteBuffer hasArray() method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the ByteBuffer hasArray() method in Java?

Find on Educative

Conclusion:

I hope this What is the ByteBuffer hasArray() method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.