How to clear a float buffer in java.


 

How to clear a float buffer in java.

In this tutorial you will see how to clear a float buffer in java.

In this tutorial you will see how to clear a float buffer in java.

How to clear a float buffer in java.

In this tutorial, we will  see how to clear a float buffer in java.

ByteBuffer API:

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

Return type Method Description
static FloatBuffer allocate( int capacity)  The allocate() method allocate a new float buffer of given capacity. 
abstract FloatBuffer put(float f) The put(..) method write a float value on the current position and increment position.
abstract float get() The get() method read float value from buffer current position and increment the position.
Buffer flip() The flip() method transfer the buffer from writing mode to reading mode.
Buffer clear() The clear() method clean a buffer, and after clear the position will be zero and limit will be capacity of buffer.

Code

import java.nio.*;
import java.nio.FloatBuffer;
public class FloatBufferClear {
  public static void main(String[] args) {
    FloatBuffer floatBuf = FloatBuffer.allocate(55);
    floatBuf.put(34.98f);
    floatBuf.flip();
System.out.print("Before clear method data in buffer.\n");
    for (int i = 0; i < floatBuf.limit(); i++) {
      System.out.print(floatBuf.get());
    }
    floatBuf.clear();
    floatBuf.put(675.87f);
    floatBuf.flip();
System.out.print("\nAfter clear method data in buffer.\n");
    for (int i = 0; i < floatBuf.limit(); i++) {
      System.out.print(floatBuf.get());
    }
  }
}

Output

C:\>java FloatBufferClear
Before clear method data in buffer.
34.98
After clear method data in buffer.
675.87

Download this code

Ads