Buffer capacity() method in Java with Examples Last Updated : 18 Jul, 2019 Comments Improve Suggest changes Like Article Like Report 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: Java // Java program to demonstrate // capacity() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(7); // putting the int to byte typecast // value in ByteBuffer bb.put((byte)20); bb.put((byte)30); bb.put((byte)40); bb.put((byte)50); // Typecasting ByteBuffer into Buffer Buffer bb1 = (Buffer)bb; // getting capacity of Buffer // using capacity() method int cap = bb1.capacity(); // display the result System.out.println("capacity is: " + cap); } } Output: capacity is: 7 Examples 2: Java // Java program to demonstrate // capacity() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring and initializing byte array byte[] byt = { (byte)20, (byte)30, (byte)40, (byte)50, (byte)60 }; // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.wrap(byt); // Typecasting ByteBuffer into Buffer Buffer bb1 = (Buffer)bb; // getting capacity of Buffer // using capacity() method int cap = bb1.capacity(); // display the result System.out.println("capacity is: " + cap); } } Output: capacity is: 5 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#capacity-- Comment More infoAdvertise with us Next Article Buffer capacity() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-Buffer Practice Tags : Java Similar Reads ByteBuffer compact() method in Java with Examples The compact() method of java.nio.ByteBuffer class is used to compact the given buffer.The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied 3 min read Buffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer 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: buf.clear(); // Prepa 3 min read Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark 3 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 ByteBuffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer 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: buf.clear(); // Prepa 2 min read ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, 3 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 Buffer flip() methods in Java with Examples The flip() method of java.nio.ByteBuffer 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 3 min read Buffer mark() methods in Java with Examples The mark() method of java.nio.Buffer Class is used to set this buffer's mark at its position.Syntax: public Buffer 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 import java. 2 min read DoubleBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.DoubleBuffer Class is used to Create a new float buffer that shares the given bufferâs content. The content of the new buffer will be that of this buffer. Changes to this bufferâs content will be visible in the new buffer, and vice versa; the two buffersâ position, 3 min read Like