CharBuffer length() methods in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

CharBuffer is : [a, b, c]
Length: 3

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.