Use of slice method of FloatBuffer class in java.


 

Use of slice method of FloatBuffer class in java.

In this tutorial you will see the use of slice method of FloatBuffer class in java.

In this tutorial you will see the use of slice method of FloatBuffer class in java.

Use of slice method of FloatBuffer class in java.

In this tutorial, we will see the use of slice method of FloatBuffer class in java.

FloatBuffer 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 float buffer of given capacity. 
abstract FloatBuffer slice() The slice() returns slice buffer that share the content of associated float buffer, and after the slicing position will be zero. 

Code

import java.nio.*;
import java.nio.FloatBuffer;
public class Test2 {
  public static final int capacity = 512;
  public static void main(String[] arg) {
    FloatBuffer f = FloatBuffer.allocate(capacity);
    f.put(22.45f);
    f.put(2.45f);
    System.out.println("Information of float buffer");
    System.out.println("Position : " + f.position());
    System.out.println("Capacity : " + f.capacity());
    System.out.println("Limit : " + f.limit());
    f.flip();
    FloatBuffer f1 = f.slice();
System.out.println("Information of slice float buffer.");
    System.out.println("Position : " + f1.position());
    System.out.println("Capacity : " + f1.capacity());
    System.out.println("Limit : " + f1.limit());
   System.out.println("value in slice float buffer.");
    while (f1.hasRemaining()) {
      System.out.println(f1.get());
    }
  }
}

Output

C:\>java SliceFloatBuffer
Information of float buffer.
Position : 2
Capacity : 512
Limit : 512
Information of slice float buffer.
Position : 0
Capacity : 2
Limit : 2
value in slice float buffer.
22.45
2.45

Download this code

Ads