anushree
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Originally posted by Dan Chisholm:
The equals operator, ==, compares the references for equality. The s3 and s4 references both refer to the same instance of a String constant so s3 and s4 contain the same reference value and the equality operator returns the boolean value "true".
The s2 reference refers to a new instance of a String object that is not the same instance as the String constant referenced by s3 and s4 so the equals operator, ==, returns false.
SCJP1.4(91%)
Originally posted by Arpana Rai:
But Dan,the replace method at line 2 should not return new string object as "arit" is already present in string pool(line 1). so line 3 must result 2 true .
regds
Arpana
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Originally posted by Dan Chisholm:
Your thinking is certainly logical but the replace method does not work that way. If the String does indeed contain the char to be replaced then the contents of the String is copied to a char array and then all occurrences of the old character are replaced by the new character. The resulting character array is then passed as an argument to the String constructor to create a new instance of a String.
If you would like the new String reference to refer to a String that already exists in the String constant pool then you must invoke the intern method on the String instance.
SCJP1.4(91%)
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Originally posted by Dan Chisholm:
The String constructor does not check the String constant pool when it creates a new String object. All String methods that return a new instance of a String use the String constructor so none of them check the String constant pool.
SCJP1.4(91%)
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
SCJP1.4(91%)
anushree
The String constructor does not check the String constant pool when it creates a new String object. All String methods that return a new instance of a String use the String constructor so none of them check the String constant pool.
anushree
Originally posted by anushree ari:
so david according to code what is your final comments about string and string pool,
thx for such a nice example,
anushree
anushree
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|