Share on Google+Share on Google+

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.


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


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

Download this code


Posted on: August 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.