Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ByteBuffer isDirect() methods in Java with Examples

  • Last Updated : 27 Jun, 2019

The isDirect() method of java.nio.ByteBuffer Class is used to tell whether or not this byte buffer is direct.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public abstract boolean isDirect()

Return Value: This method returns true if, and only if, this buffer is direct.

Below are the examples to illustrate the isDirect() method:



Examples 1:




// Java program to demonstrate
// isDirect() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating ByteBuffer
        // using allocate() method
        ByteBuffer byteBuffer
            = ByteBuffer.allocateDirect(4);
  
        // check the byteBuffer
        // using isDirect() method
        boolean val = byteBuffer.isDirect();
  
        // checking the condition
        if (val)
            System.out.println("buffer is direct");
        else
            System.out.println("buffer is not direct");
    }
}
Output:
buffer is direct

Examples 2:




// Java program to demonstrate
// isDirect() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating ByteBuffer
        // using allocate() method
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  
        // check the byteBuffer
        // using isDirect() method
        boolean val = byteBuffer.isDirect();
  
        // checking the condition
        if (val)
            System.out.println("buffer is direct");
        else
            System.out.println("buffer is not direct");
    }
}
Output:
buffer is not direct

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#isDirect–




My Personal Notes arrow_drop_up
Recommended Articles
Page :