String s1="123";//在字符串池中
String s3 = new String("123");//分配一个内存空间String s2 = new String();//分配一个内存空间
s2="123";//是否在字符串池中分配?System.out.println(s1==s2);//为什么是true?

解决方案 »

  1.   

    Both s1 and s2 refer to "123" in constant pool. That's why s1 == s2 returns true.
      

  2.   

    写错了,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,String  s2  =  new  String(); //分配一个内存空间 
    s2 = "123 "; //是不是自动指向constant pool中的 "123" ?
    为什么内存为S2划分了内存空间了,还要自动指向constant pool中的"123"?
    还有一点:
    constant pool到底是一个怎么样的机制?
    有说法是:保存在已编译的.class文件中的一些数据??是这样吗??谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  3.   

    Quotes from a book:
    ----------------------------------------------------------------------------------------
    "One of the key goals of any good programming language is to make efficient use of memory. As applications grow, it's very common for String literals to occupy large amounts of a program's memory, and there is often a lot of redundancy within the universe of String literals for a program. To make Java more memory efficient, the JVM sets aside a special area of memory called the "String constant pool." When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.) Now we can start to see why making String objects immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String's value.You might say, "Well that's all well and good, but what if someone overrides the String class functionality? Couldn't that cause problems in the pool?" That's one of the main reasons that the String class is ed final. Nobody can override the behaviors of any of the String methods, so you can rest assured that the String objects you are counting on to be immutable will, in fact, be immutable."Creating New Strings
    Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let's look at a couple of examples of how a String might be created, and let's further assume that no other String objects exist in the pool:String s = "abc";     //  creates one String object and one
                          //  reference variableIn this simple case, "abc" will go in the pool and s will refer to it.String s = new String("abc");  // creates two objects,
                                   // and one reference variableIn this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.
    -----------------------------------------------------------------------------------------------------Do you still have questions? Please read books and DO READ GOOD BOOKS. :-)
      

  4.   

    Learned more...
    z_lping 回答得已经相当清楚了.
    欲了解更多,
    http://topic.csdn.net/t/20050312/17/3845819.html