FloatBuffer clear() methods in Java with Examples Last Updated : 10 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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-- Comment More infoAdvertise with us Next Article FloatBuffer compact() method in Java With Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-FloatBuffer Java-NIO package Practice Tags : Java Similar Reads FloatBuffer compact() method in Java With Examples The compact() method of java.nio.FloatBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is se 3 min read FloatBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of float 4 min read FloatBuffer equals() method in Java with Examples The equals() method of java.nio.FloatBuffer Class is used to check whether or not the given buffer is equal to another object. Two float buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, c 4 min read FloatBuffer allocate() method in Java With Examples The allocate() method of java.nio.FloatBuffer Class is used to allocate a new float buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing arr 2 min read FloatBuffer get() methods in Java with Examples get() The get() method of java.nio.FloatBuffer Class is used to reads the float at the given buffer's current position, and then increments the position. Syntax : public abstract float get() Return Value: This method returns the float value at the buffer's current position. Throws: This method throw 5 min read FloatBuffer put() methods in Java with Examples put(float f) The put(float f) method of java.nio.FloatBuffer Class is used to write the given float into the newly created float buffer at the current position, and then increments the position. Syntax : public abstract FloatBuffer put(float f) Parameters: This method takes the float value f as a pa 6 min read Like