I have read about this in Mala Gupta's books and also read in Kathy Sierra and got confused. I think as String objects are immutable so this should also go in constant pool and later could be reused if a new object is being created as below.
Don't understand, but that looks to me like a different question. The original question was whether the String "Java" goes into the String pool when that code is executed.
I guess, yes I was confused. I am still learning basics of java.
Thanks for the links, so the objects are not created in the pool when created with new operator. But the literal value of the string will be kept in the string pool. This helps.
No, that String is already in the String pool before that line runs; it is put into the String pool the first time a class containing that particular String literal (or an equal constant expression) is loaded.
Line 2 is a simple assignment; as you know the String already exists in the String pool.