--------------<br />Prashant<br />SCJP-91%
giddee up
--------------<br />Prashant<br />SCJP-91%
So, to add a String to the pool, you intern it. Now, if you intern object S, ABC will be added to the pool (assuming new doesn't add ABC to pool when you initialize S).When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
If this true, Prashanth and others, I agree answer to the original post would be 1(one). WHY WOULD I EVER NEED TO INTERN A STRING??? just to get a reference from the pool??? Why would API state "Otherwise, this String object is added to the pool and a reference to this String object is returned" ? The string would already be in the pool!!!For the exam, you need to know that IF you use 'new' to create a new String that is not *already* in the pool, TWO String objects are created. But there is no need to ever have duplicates in the String pool, so if the String is already in the pool at the time you use 'new', there will be only ONE created.
Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|