String是引用类型,多次改变值造成资源浪费StringBuffer效律高

解决方案 »

  1.   

    在处理 String 的时候要尽量使用 StringBuffer 类,StringBuffer 类是构成 String 类的基础。String 类将 StringBuffer 类封装了起来,(以花费更多时间为代价)为开发人员提供了一个安全的接口。当我们在构造字符串的时候,我们应该用 StringBuffer 来实现大部分的工作,当工作完成后将 StringBuffer 对象再转换为需要的 String 对象。比如:如果有一个字符串必须不断地在其后添加许多字符来完成构造,那么我们应该使用 StringBuffer 对象和她的 append() 方法。如果我们用 String 对象代替 StringBuffer 对象的话,会花费许多不必要的创建和释放对象的 CPU 时间。
      

  2.   

    注意,是在对字符串本身进行构造的时候,最好用stringbuffer
      

  3.   

    String不能改变大小,StringBuffer可以。
    你可以看看JDK的文档,说得很详细。
      

  4.   

    个人认为  :大部分还是 stringbuffer好用,  一般都转化成  stringbuffer
    大家都来谈谈  意见~
      

  5.   

    多谢各位
    我知道String不能改变大小,但是String不是也可以用 + 号连接么(是不是这个过程就是创建了新的String对象?)?这个大小到底是什么含义呢?haode(好的) ,大致明白了你的意思。如果只对某些可能发生非常有限的变化的字符串进行构造的时候呢?总觉得StringBuffer写代码的时候显得非常累赘。
      

  6.   

    "+",两个字符串的连接是拷贝到一个临时的字符串缓冲区,然后再拷回去的。
    这种拷贝过程的代价是相当大的。因此一个基本的性能原则是如果需要构造一个字符串,应明确地使用StringBuffer对象。类似+和+=这样的字符串连接操作偶尔使用是很好的,但是它们的代价是很高的。
      

  7.   

    运算 示例 标准时间本地赋值 i=n; 1.0
    实例赋值 this.i=n; 1.2
    int增值 i++; 1.5
    byte增值 b++; 2.0
    short增值 s++; 2.0
    float增值 f++; 2.0
    double增值 d++; 2.0
    空循环 while(true) n++; 2.0
    三元表达式 (x<0) ?-x : x 2.2
    算术调用 Math.abs(x); 2.5
    数组赋值 a[0] = n; 2.7
    long增值 l++; 3.5
    方法调用 funct(); 5.9
    throw或catch异常 try{ throw e; }或catch(e){} 320
    同步方法调用 synchMehod(); 570
    新建对象 new Object(); 980
    新建数组 new int[10]; 3100
    字串的开销:字串连接运算符+看似简单,但实际需要消耗大量系统资源。编译器可高效地连接字串,但变量字串却要求可观的处理器时间。例如,假设s和t是字串变量:
    System.out.println("heading" + s + "trailer" + t);
    上述语句要求新建一个StringBuffer(字串缓冲),追加自变量,然后用toString()将结果转换回一个字串。因此,无论磁盘空间还是处理器时间,都会受到严重消耗。若准备追加多个字串,则可考虑直接使用一个字串缓冲——特别是能在一个循环里重复利用它的时候。通过在每次循环里禁止新建一个字串缓冲,可节省980单位的对象创建时间(如前所述)。利用substring()以及其他字串方法,可进一步地改善性能。如果可行,字符数组的速度甚至能够更快。也要注意由于同步的关系,所以StringTokenizer会造成较大的开销。
      

  8.   

    多谢~
    那么如果用String对象的concat()方法把字符串拼接起来又如何呢?
      

  9.   

    那么如果用String对象的concat()方法把字符串拼接起来又如何呢?
    这样作的结果是生成一个新的字符串,但原来的字符串不变!
      

  10.   

    是不是就是说也花了额外的开销生成了新的对象,其效率同样不如直接用StringBuffer来得好?
      

  11.   

    那么什么地方只能用StringBuffer的
    而不能 用String的?????
      

  12.   

    《Effective Java Programming Language Guide》  Item 33: Beware the performance of string concatenationThe string concatenation operator (+) is a convenient way to combine a few strings into one. It is fine for generating a single line of output or for constructing the string representation of a small, fixed-size object, but it does not scale. Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n. It is an unfortunate consequence of the fact that strings are immutable (Item 13). When two strings areconcatenated, the contents of both are copied.For example, consider the following method that constructs a string representation of a billing statement by repeatedly concatenating a line for each item:// Inappropriate use of string concatenation - Performs horribly!
       public String statement() {
           String s = "";
           for (int i = 0; i < numItems(); i++)
               s += lineForItem(i); // String concatenation
           return s;
       }This method performs abysmally if the number of items is large. To achieve acceptableperformance, use a StringBuffer in place of a String to store the statement underconstruction:    public String statement() {
            StringBuffer s = new StringBuffer(numItems() * LINE_WIDTH);
            for (int i = 0; i < numItems(); i++)
                s.append(lineForItem(i));
            return s.toString();
        }The difference in performance is dramatic. If numItems returns 100 and lineForItem returns a constant 80-character string, the second method is ninety times faster on my machine than the first. Because the first method is quadratic in the number of items and the second is linear, the performance difference is even more dramatic for larger numbers of items. Note that the second method preallocates a StringBuffer large enough to hold the result. Even if it is detuned to use a default-sized StringBuffer, it is still forty-five times faster than the first.The moral is simple: Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuffer's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.
      

  13.   

    我觉得concat()方法跟String的+运算是等同的
      

  14.   

    When two strings areconcatenated, the contents of both are copied.
    String s1="adfasd";
    String s2="oiuioqer";
    s1=s1+s2;
    也就是说s1,s2被拷贝到一块内存区然后合并再赋给一个新的内存区s1(新创建的一个对象)?几位朋友还是未明确说明
      

  15.   

    呵呵,你可以做一个试验.
    StringBuffer sb;
    String s;
    然后让sb循环十万遍加一个字符串
    让s循环同样的次数加同样的字符串
    然后打印各自用的时间.
    你会发现,StringBuffer比String快 成千上万倍 !!!
    呵呵,具体是几千还是几万我忘了 :) ------------------------------------------------------
               我们还年轻牛奶会有的奶牛也会有的 
                 可天天在 csdn 混这些会有吗 ??
      

  16.   

    我的意见还是可以用StringBuffer的地方尽量使用它,注意的是互相赋值的时候是把prefence给它,StringBuffer s1 = new StringBuffer("");
    StringBuffer s2 = new StringBuffer("");
    s2 = s1;于是s2会随着s1的改变而改变,但是String不会===================================
       我的一分耕耘,你能给一分收获
      

  17.   

    楼上都讲的很清楚~~~我学到了很多,谢谢我很少用StringBuffer的,对字符串的处理基本上都是用String解决,艾,现在菜发现编码效率极低