stringbuffer 原理string 区别

stringbuffer  调用append()  通过扩容和copy


一:
        expandCapacity(j);  //内部通过copyof构建含有原字符数组的 新的长度的字符串
二:
        paramStringBuffer.getChars(0, i, this.value, this.count);  将追加长度为i的字符串追加到 value类型为char的数组的原(


保存)长度的后面并返回。




频繁操作字符串,不会生成大量string对象 只会在stringbuffer tostring()方法是构造一个string s  = new String(value,0,count);
value ---  char类型的字符串 

count ---  char中真是的数据的长度


String test = "test";
Class classes = test.getClass();
Field ff =  classes.getDeclaredField("count");
ff.setAccessible(true);
System.out.println(ff.get(test));   //count = 4


public final class String
  implements Serializable, Comparable, CharSequence
{
  private final char[] value;
  private final int offset;
  private final int count;
  private int hash;
  private static final long serialVersionUID = -6849794470754667710L;
  private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];
  public static final Comparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(null);

你可能感兴趣的:(java)