CharBuffer length() methods in Java with Examples Last Updated : 23 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The length() method of java.nio.CharBuffer Class is used to return the length of this character buffer. When viewed as a character sequence, the length of a character buffer is simply the number of characters between the position (inclusive) and the limit (exclusive); that is, it is equivalent to remaining(). Syntax: public final int length() Return Value: This method returns the length of this character buffer. Below are the examples to illustrate the length() method: Examples 1: Java // Java program to demonstrate // length() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declare and initialize the char array char[] cb = { 'a', 'b', 'c' }; // wrap the char array into CharBuffer // using wrap() method CharBuffer charBuffer = CharBuffer.wrap(cb); // Get the length of the charBuffer // using length() method int length = charBuffer.length(); // print the byte buffer System.out.println("CharBuffer is : " + Arrays.toString( charBuffer.array()) + "\nLength: " + length); } } Output: CharBuffer is : [a, b, c] Length: 3 Examples 2: Java // Java program to demonstrate // length() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // defining and allocating CharBuffer // using allocate() method CharBuffer charBuffer = CharBuffer.allocate(4); // append char value in charBuffer // using append() method charBuffer.append('a'); charBuffer.append('b'); // print the char buffer System.out.println("CharBuffer before append : " + Arrays.toString( charBuffer.array()) + "\nLength: " + charBuffer.length()); // append char value in charBuffer // using append() method charBuffer.append('c'); // print the char buffer System.out.println("\nCharBuffer after append : " + Arrays.toString( charBuffer.array()) + "\nLength: " + charBuffer.length()); } } Output: CharBuffer before append : [a, b,, ] Length: 2 CharBuffer after append : [a, b, c, ] Length: 1 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#length-- 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 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 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 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 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 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 Like