Buffer capacity() method in Java with Examples
Last Updated :
18 Jul, 2019
The capacity() method of java.nio.Buffer Class is used to return this buffer’s capacity.
Syntax:
public final int capacity()
Return Value: The capacity of this buffer
Below are the examples to illustrate the capacity() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ByteBuffer bb = ByteBuffer.allocate( 7 );
bb.put(( byte ) 20 );
bb.put(( byte ) 30 );
bb.put(( byte ) 40 );
bb.put(( byte ) 50 );
Buffer bb1 = (Buffer)bb;
int cap = bb1.capacity();
System.out.println( "capacity is: "
+ cap);
}
}
|
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
byte [] byt = { ( byte ) 20 , ( byte ) 30 ,
( byte ) 40 , ( byte ) 50 ,
( byte ) 60 };
ByteBuffer bb = ByteBuffer.wrap(byt);
Buffer bb1 = (Buffer)bb;
int cap = bb1.capacity();
System.out.println( "capacity is: "
+ cap);
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#capacity–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...