Use of putDouble() and getDouble() method of ByteBuffer class.


 

Use of putDouble() and getDouble() method of ByteBuffer class.

In this tutorial you will see the use of putDouble() and getDouble() method of ByteBuffer class.

In this tutorial you will see the use of putDouble() and getDouble() method of ByteBuffer class.

Use of putDouble() and getDouble() method of ByteBuffer class.

 In this tutorial, we will see how to write and read double value from a byte buffer.

ByteBuffer API:

The java.nio.ByteBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
static ByteBuffer allocate(int capacity)  The allocate(..)method allocate a new byte buffer.
abstract ByteBuffer putDouble() The puDouble() method writes 8 byte containing the given double value into associated buffer.
abstract float getDouble() The getDouble() method read 8 byte from current position and increment position by 8.

code

import java.nio.*;
import java.nio.ByteBuffer;

public class GetDoubleValue {
  public static final int capacity = 256;

  public static void main(String[] args) throws Exception {
    ByteBuffer bytebuf = ByteBuffer.allocateDirect(capacity);
    bytebuf.putDouble(5822.0876);
    bytebuf.flip();
    System.out.println("Double data into byte buffer : "
        + bytebuf.getDouble());
  }
}

Output

C:\>java GetDoubleValue
Double data into byte buffer : 5822.0876

Download this code

Ads