CharBuffer clear() methods in Java with Examples
The clear() method of java.nio.CharBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer.
For example:
// Prepare buffer for reading buf.clear(); // Read data in.read(buf);
This 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 that might as well be the case.
Syntax:
public final CharBuffer clear()
Return Value: This method returns this buffer.
Below are the examples to illustrate the clear() method:
Examples 1:
// Java program to demonstrate // clear() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { try { char[] carr = { 'a', 'b', 'c', 'd' }; // creating object of CharBuffer // and allocating size capacity CharBuffer cb = CharBuffer.wrap(carr); // try to set the position at index 2 cb.position(2); // Set this buffer mark position // using mark() method cb.mark(); // try to set the position at index 4 cb.position(4); // display position System.out.println("position before reset: " + cb.position()); // try to call clear() to restore // to the position at index 0 // by discarding the mark cb.clear(); // display position System.out.println("position after reset: " + cb.position()); } catch (InvalidMarkException e) { System.out.println("new position is less than " + "the position we " + "marked before "); System.out.println("Exception throws: " + e); } } } |
position before reset: 4 position after reset: 0
Examples 2:
// Java program to demonstrate // clear() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { char[] carr = { '.', '<', '@', 'a' }; // creating object of CharBuffer // and allocating size capacity CharBuffer cb = CharBuffer.wrap(carr); // try to set the position at index 2 cb.position(3); // display position System.out.println("position before clear: " + cb.position()); // try to call clear() to restore // to the position at index 0 // by discarding the mark cb.clear(); // display position System.out.println("position after clear: " + cb.position()); } } |
position before clear: 3 position after clear: 0
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#clear–
Recommended Posts:
- CharBuffer append() methods in Java with Examples
- CharBuffer rewind() methods in Java with Examples
- CharBuffer charAt() methods in Java with Examples
- CharBuffer chars() methods in Java with Examples
- CharBuffer limit() methods in Java with Examples
- CharBuffer order() methods in Java with Examples
- CharBuffer length() methods in Java with Examples
- CharBuffer position() methods in Java with Examples
- CharBuffer subSequence() methods in Java with Examples
- CharBuffer flip() methods in Java with Examples
- CharBuffer reset() methods in Java with Examples
- CharBuffer read() methods in Java with Examples
- CharBuffer mark() methods in Java with Examples
- FloatBuffer clear() methods in Java with Examples
- Buffer clear() methods in Java with Examples
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.

