CharBuffer chars() methods in Java with Examples Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined. Syntax: public IntStream chars() Return Value: This method returns an IntStream of char values from this sequence. Below are the examples to illustrate the chars() method: Example 1: Java // Java program to demonstrate // chars() method import java.nio.*; import java.util.*; import java.util.stream.IntStream; public class GFG { public static void main(String[] args) { // creating object of CharBuffer // and allocating size capacity CharBuffer charbuffer = CharBuffer.allocate(3); // append the value in CharBuffer // using append() method charbuffer.append('a') .append('b') .append('c') .rewind(); // print the CharBuffer System.out.println("Original CharBuffer: " + Arrays.toString( charbuffer.array()) + "\n"); // Read char at particular Index // using chars() method IntStream stream = charbuffer.chars(); // Display the stream of int zero-extending // the char values from this sequence stream.forEach(System.out::println); } } Output:Original CharBuffer: [a, b, c] 97 98 99 Example 2: Java // Java program to demonstrate // chars() method import java.nio.*; import java.util.*; import java.util.stream.IntStream; public class GFG { public static void main(String[] args) { // creating object of CharBuffer // and allocating size capacity CharBuffer charbuffer = CharBuffer.allocate(5); // append the value in CharBuffer // using append() method charbuffer.append((char)140) .append((char)117) .append((char)118) .append((char)0) .append((char)90) .rewind(); // print the CharBuffer System.out.println("Original CharBuffer: " + Arrays.toString( charbuffer.array()) + "\n"); // Read char at particular Index // using chars() method IntStream stream = charbuffer.chars(); // Display the stream of int zero-extending // the char values from this sequence stream.forEach(System.out::println); } } Output:Original CharBuffer: [?, u, v, , Z] 140 117 118 0 90 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#chars-- Comment More infoAdvertise with us Next Article CharBuffer mark() methods in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-CharBuffer Java-NIO package Practice Tags : Java Similar Reads 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 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 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 flip() methods in Java with Examples The flip() method of java.nio.CharBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of 2 min read CharBuffer limit() methods in Java with Examples The limit() method of java.nio.CharBuffer Class is used to set this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded. Syntax: public CharBuffer limit(int newLimit) Return Value: Thi 2 min read CharBuffer order() methods in Java with Examples The order() method of java.nio.CharBuffer class is used to retrieve this buffer's byte order. The byte order of a char buffer created by allocation or by wrapping an existing char array is the native order of the underlying hardware. The byte order of a char buffer created as a view of a byte buffer 2 min read Like