集册 Java实例教程 演示由String,StringBuffer和CharBuffer实现的java.lang.CharSequence的行为。

演示由String,StringBuffer和CharBuffer实现的java.lang.CharSequence的行为。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

542
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
演示由String,StringBuffer和CharBuffer实现的java.lang.CharSequence的行为。

import java.nio.CharBuffer;

/*来自 
 NowJava.com - 时代Java*/

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);

    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");/** nowjava - 时代Java 提供 **/

    charBuffer.reset();

    printCharSequence(charSequence);

    charBuffer.clear();

    printCharSequence(charSequence);

  }

  private static 
展开阅读全文