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<String>, 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<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(null);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章