Demonstrate behavior of java.lang.CharSequence as implemented by String, StringBuffer and CharBuffer. - Java File Path IO

Java examples for File Path IO:CharBuffer

Description

Demonstrate behavior of java.lang.CharSequence as implemented by String, StringBuffer and CharBuffer.

Demo Code

import java.nio.CharBuffer;

public class Main {
  public static void main(String[] argv) {
    StringBuffer stringBuffer = new StringBuffer("Hello World");
    CharBuffer charBuffer = CharBuffer.allocate(20);
    CharSequence charSequence = "Hello World";
    printCharSequence(charSequence);//from  w  w  w .  ja  va 2 s  . co m
    charSequence = stringBuffer;
    printCharSequence(charSequence);
    stringBuffer.setLength(0);
    stringBuffer.append("hi");
    printCharSequence(charSequence);
    charSequence = charBuffer;
    charBuffer.put("test");
    charBuffer.clear();
    charBuffer.put("Hello World");
    charBuffer.flip();
    printCharSequence(charSequence);
    charBuffer.mark();
    charBuffer.put("asdf");
    charBuffer.reset();
    printCharSequence(charSequence);
    charBuffer.clear();
    printCharSequence(charSequence);
  }
  private static void printCharSequence(CharSequence cs) {
    System.out.println("length=" + cs.length() + ", content='" + cs.toString()
        + "'");
  }
}

Result


Related Tutorials