jdk1.5 中已经有 StringBuffer 的替代品 StringBuilder了~

解决方案 »

  1.   

    String复写了equals()
    StringBuffer没有,还是Object的
      

  2.   

    http://aeonsun.polyhero.net/showlog.jspe?cat_id=-1&log_id=20着篇文章写了string是不可变的 那stringbuffer呢 谁知道他的机理啊 如果是可变的 那如何工作呢
      

  3.   

    StringBuffer提供对字符串的动态编辑。
      

  4.   

    String 是final的,意味着每次赋值都会新产生一个新的对象并指向这个对象,而原来的对象在内存种其实还是存在的。理论上来说,这样是浪费内存,当然了,实际上大都是微乎其微的。
    StringBuffer则不一样,它指向的永远都是原来的那块内存地址
      

  5.   

    ^_^,StringBuffer提供对字符串的动态编辑。这句好
      

  6.   

    同步:就是大家按照一定的规则访问(一般来说是FIFO);
    异步:并发访问。从以上可以看来,同步/异步的问题只有在多线程下才会出现。来自java文档:
    String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. 也就是说:StringBuffer 的所有方法是同步的,多线程访问下也是安全的。而String是非同步的,那是否可以说String就是不安全的呢?我的看法:String也是安全的,因为String的操作根本不会改变此字符串。
      

  7.   

    关于速度上的差异,这个我深有感触。这可能也是非常关键的不同:
    String s = "";
    for(int i=0; i<10000; i++) {
    s += "item" + i;
    }
    的运行需要10秒多,而:
    StringBuffer sb = new StringBuffer();
    for(int i=0; i<10000; i++) {
    sb.append("item" + i);
    }
    基本上不需要什么时间。而数据量很大时就更加明显!!
      

  8.   

    建议楼主看看API或原文件,这都是很基础的,String是不能更改其内容,所以有没有synchronized都是安全的,StringBuffer中所有的public方法都有synchronized所有多线程也是安全的