CharBuffer clear() methods in Java with Examples Last Updated : 23 Jul, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } } Output: position before reset: 4 position after reset: 0 Examples 2: Java // 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()); } } Output: position before clear: 3 position after clear: 0 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#clear-- Comment More infoAdvertise with us Next Article CharBuffer clear() methods in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-CharBuffer Java-NIO package Practice Tags : Java Similar Reads CharBuffer chars() methods in Java with Examples The chars() method of java.nio.CharBuffer Class is used to return a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted. The stream binds to this sequence when the terminal stream operation commences (specific 2 min read Buffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer 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: buf.clear(); // Prepa 3 min read ByteBuffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer 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: buf.clear(); // Prepa 2 min read CharBuffer mark() methods in Java with Examples The mark() method of java.nio.CharBuffer Class is used to set this buffer's mark at its position. Syntax: public CharBuffer mark() Return Value: This method returns this buffer. Below are the examples to illustrate the mark() method: Examples 1: Java // Java program to demonstrate // mark() method i 2 min read CharBuffer read() methods in Java with Examples The read() method of java.nio.CharBuffer Class is used to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. Syntax: public int rea 4 min read Like