Thursday, October 9, 2025
HomeLanguagesJavaCharBuffer clear() methods in Java with Examples

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


Output:

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


Output:

position before clear: 3
position after clear: 0

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

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS