Home Tutorial Java Corejava Nio Use of putDouble() and getDouble() method of ByteBuffer class.

 
 

Share on Google+Share on Google+
Use of putDouble() and getDouble() method of ByteBuffer class.
Posted on: July 28, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for Use of putDouble() and getDouble() method of ByteBuffer class.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 28, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com