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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

position before reset: 4
position after reset: 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

position before clear: 3
position after clear: 0

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.