参考我以前的一个贴子:
String a ="hello" 创建了一个String对象,JAVA中会对每个字符串常量自动解析成一个String对象.
String a =new String("hello")创建了两个String对象,自动解析了一个,用new关键字创建了一个,后面的这个交给a来引用.
试验程序如下:
public class TestString {
    
    /** Creates a new instance of TestString */
    public TestString() {
    }
    
    public static void main(String[] args) {
        // TODO code application logic here
        int i = 0;
        String a;
        while(true)
        {
            a = new String("aa" + i++);//1
      //      a = "aa" + i++;  //2
            try {
                Thread.sleep(3000);
            } catch(Exception e) {}
        }
    }
    
}
这个程序运行后用profiler来查看就可以清楚的看到String对象增长的情况,语句一是一次两个的增长,改用语句二是一次一个的增长.这个是我用工具观察得出来的结论, 那么是不是就可以说, 为了不必要的内存开销, 我们在任何情况下都不需要用new去创建一个String对象.

解决方案 »

  1.   

    String 操作时用StringBuffer 会不会更好一点
      

  2.   

    也不一定,为什么会有一个new String(String original)这个方法呢,这是因为用这个方法创建的对象是独立的一个对象,比如
    1.String a = "hello";
    2.String b = new String(a);
    那么a和b就是两个不同的对象,
    而如果把2改成:String b = a;
    那么b和a就是指向同一个对象的。
      

  3.   


    在用字节数组时需要用到
    String s= new String(char [])
      

  4.   

    我的理解是String a = "hello";是在编译时就编译到常量中了(字符串池),放在栈中,而String b = new String(a);是在运行中创建的string对象,放在堆中,所以a!=b;
    比如:String ab="ab";String a_b="a"+"b";
    那么ab==a_b;属第一种.
      

  5.   

    理由一 coolarmy(bb考拉猪): 也不一定,为什么会有一个new String(String original)这个方法呢,这是因为用这个方法创建的对象是独立的一个对象
    理由二 725137(2006年不会菜):在用字节数组时需要用到String s= new String(char []).
    其实这一条是可以扩展的,也就是说需要参数的时候还是要用new String的.还有什么其他的情况必须要用吗?<Effective JAVA>第4条是什么了, 这本书我没读过, 能简单解释一下吗?
      

  6.   

    http://www.eclipse.org/eclipse/development/performance/bloopers.htmlThe java.lang.String.substring(...) method is usually implemented by creating a new String object that points back to the same underlying char[]  as the receiver, but with different offset and length values. Therefore, if you take a very large string, create a substring of length 1, then discard the large string, the little substring may still hold onto the very large char[].A nasty variant of this blooper is when the substring is later interned by calling String.intern(). On some VMs, this means the large char[] object is now held onto forever by the VM's intern pool. Kiss that memory good-bye, because there's no way to free it again.Avoidance techniques:
    In situations where you know you are creating a small substring and then throwing the large string away, force a copy of the string to be created by calling new String(substring). This seems counter-intuitive from a performance perspective because it creates extra objects, but it can be worthwhile if the substring is being retained for a long period. In one particular case in the Eclipse JDT plugins, copying the substring yielded a 550KB space savings. Not bad for a one line fix!
      

  7.   

    看了楼上的贴子,受益匪浅, 看来用subString的时候如果不用new String的话是会有内存泄露的危险的. 长见识了.
    还有没有其他的情况了, 我会尽快结贴.
      

  8.   

    new String()默认是造一个新对象,不过你可以看看new String("hello").intern()方法的说明。