Home Tutorial Java Corejava Nio Use of getFloat() method of ByteBuffer class in java.

 
 

Use of getFloat() method of ByteBuffer class in java.
Posted on: July 28, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of getFloat() method of ByteBuffer class in java.

Use of getFloat() method of ByteBuffer class in java.

 In this tutorial, we will see how to read float 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 putFloat() The putFloat(..) method write four byte containing the given float value into associated buffer.
abstract float getFloat() The getFloat() method read four byte from current position and increment position by four.

code

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

public class GetFloatValue {
  public static final int capacity = 100;
  public static void main(String[] argvthrows Exception {
    ByteBuffer bytebuf = ByteBuffer.allocate(capacity);
    bytebuf.putFloat(221255.02F);
    bytebuf.flip();
    System.out.println("Float data into byte buffer : "
        + bytebuf.getFloat());
  }
}

Output

C:\>java GetFloatValue
Float data into byte buffer : 221255.02

Download this code

Advertisement

Related Tags for Use of getFloat() method of ByteBuffer class in java.:


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