CharBuffer length() methods in Java with Examples
Last Updated :
23 Jul, 2019
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
char [] cb = { 'a' , 'b' , 'c' };
CharBuffer charBuffer = CharBuffer.wrap(cb);
int length = charBuffer.length();
System.out.println( "CharBuffer is : "
+ Arrays.toString(
charBuffer.array())
+ "\nLength: "
+ length);
}
}
|
Output:
CharBuffer is : [a, b, c]
Length: 3
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
CharBuffer charBuffer
= CharBuffer.allocate( 4 );
charBuffer.append( 'a' );
charBuffer.append( 'b' );
System.out.println( "CharBuffer before append : "
+ Arrays.toString(
charBuffer.array())
+ "\nLength: "
+ charBuffer.length());
charBuffer.append( 'c' );
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–
Share your thoughts in the comments
Please Login to comment...