个人理解:
String我们可以把它看做是char的数组,也就是数组里面的不同位置引用相应的char的内存地址,如果把产生的一个个的char都缓存起来的话那么自然可以节省创建String的时间,同时也节省内存的开销。以上是个人理解,等大牛给出真正答案

解决方案 »

  1.   

    以下是个人理解,说的不好,请轻拍:
    java中有个常量池,里面存放着一些常用量,比如数字存放1-128(好像是这个区间内的数字吧)之间数字,当你在java代码中写的常量值,如字符串等,在虚拟机加载java类的时候,会把这些常量值放入常量池中,
    所以String a ="nihao"; String b="nihao";中a=b为 true,应为字符串"nihao"已经存放在常量池中,
    a,b引用时使用的是同一个字符串。
    总的来说,使用常量池是为了合理的使用内存,提高性能。
      

  2.   

    A JVM has the Pool. All the String Object which are created by assignment stored in the pool. This pool is present in the heap. So whenever any assignment is done for String first it check in the String Pool whether that String is already exist or not... This is done by calling intern() method present in the String class. If it find the same String then it return the same reference else it create new 1. But with new Operator everytime it creates the new Object.
    eg.
    String str = "S1" // first check wheather its present in the poolString str1 ="S1"So now str and str1 have the same reference so .equals and == method will return true.String str2 = new String("S1");// this will create the new object in heap.So above only 2 objects are created1 by str and other by str2
      

  3.   

    能节约内存,不是new 新字符串都是会到字符串池 里去找,如果有就直接引用 
    其他作用不太清楚
      

  4.   

    建议你看下《java 学习笔记》一个台湾人写的,如果,我没有记错的话,里面谈到了这个问题,好像是为了节省空间采用的,当具体细节,请参考第126页左右有详细描述。