Hi, I have a doubt that, Will string pool can not have any duplicate entries? Please go through the below part of the programme, 1. String s1= "ABC"; 2. String s2=s1.toLowerCase(); 3. String s3="abc"; 4. boolean b = s3== s2; Now will the value of b is true or false. when i compiled i am getting it as false, i.e. s3 are s2 are two different string each one is holding the value, "abc". My doubt is where will s2 will store, will it be in string pool or some where. If it is in string, so it will have two entries of "abc". Pleae some one explain me.
If I understood it correctly, string pool will hold only string literal values, i.e. when you say String s="abc", that WILL go to the string pool. But anything else will go to the heap, i.e. String s1=s+"d" and most likely result of the .toLowerCase() method.
Using a String literal, such as "abc", will cause a new entry to be created in the String literal pool. (Makes sense, right? String literals go into the String literal pool. ) However, when you invoke a method such as toLowerCase(), which returns a new Sting, we create a brand new String object on the heap - not in the String literal pool. If you wanted to optimize your code, you could do this:
Theintern method will check the String literal pool and determine if an identical String already exists. If it does, a reference to that String object is returned so that this one (which is on the heap) can be garbage collected. However, none of this is on the SCJP exam.