Thursday, November 20, 2025
HomeLanguagesJavaFloatBuffer clear() methods in Java with Examples

FloatBuffer clear() methods in Java with Examples

The clear() method of java.nio.FloatBuffer Class is used to clear this buffer. This method set the position and limit equal to zero and capacity respectively and discard the mark. This method should be invoked when there is any necessity of sequence of channel-read or put operations. It means that if the buffer needs to be read then clear() method makes the buffer ready and set the position to zero. For example:

buf.clear();     // Prepare buffer for reading
in.read(buf);    // Read data

The method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which deletion might as well be the case.

Syntax:

public final FloatBuffer clear()

Parameters: The method does not take any parameters.

Return Value: This method returns this FloatBuffer instance after clearing all the data from it.

Below are the examples to illustrate the clear() method:

Example 1:




// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            float[] farr = { 2.5f, 3.5f, 4.5f, 6.7f };
  
            // creating object of FloatBuffer
            // and allocating size capacity
            FloatBuffer fb
                = FloatBuffer.wrap(farr);
  
            // try to set the position at index 2
            fb.position(2);
  
            // Set this buffer mark position
            // using mark() method
            fb.mark();
  
            // try to set the position at index 4
            fb.position(4);
  
            // display position
            System.out.println("position before reset: "
                               + fb.position());
  
            // try to call clear() to restore
            // to the position at index 0
            // by discarding the mark
            fb.clear();
  
            // display position
            System.out.println("position after reset: "
                               + fb.position());
        }
  
        catch (InvalidMarkException e) {
            System.out.println("new position is less than "
                               + "the position we "
                               + "marked before ");
            System.out.println("Exception throws: " + e);
        }
    }
}


Output:

position before reset: 4
position after reset: 0

Example 2:




// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        float[] farr = { 2.4f, 105.4f, 13.9f, 23.45f };
  
        // creating object of FloatBuffer
        // and allocating size capacity
        FloatBuffer fb = FloatBuffer.wrap(farr);
  
        // try to set the position at index 3
        fb.position(3);
  
        // display position
        System.out.println("position before clear: "
                           + fb.position());
  
        // try to call clear() to restore
        // to the position at index 0
        // by discarding the mark
        fb.clear();
  
        // display position
        System.out.println("position after clear: "
                           + fb.position());
    }
}


Output:

position before clear: 3
position after clear: 0

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#clear–

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS