Java CharBuffer.compact()

Syntax

CharBuffer.compact() has the following syntax.

public abstract CharBuffer compact()

Example

In the following code shows how to use CharBuffer.compact() method.


//from   ww  w.ja  v a2s. c  o m
import java.nio.CharBuffer;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(50);
    cb1.append("java2s.com");
    cb1.rewind();

    System.out.println(cb1.charAt(2));
    System.out.println(Arrays.toString(cb1.array()));

    
    CharBuffer cb2 = cb1.compact();
    System.out.println(Arrays.toString(cb2.array()));
    

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.nio »




Buffer
ByteBuffer
ByteOrder
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
MappedByteBuffer
ShortBuffer