This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I know that Strings are immutable. But practically that is not the case.I am able to change the original String. If I assign another String value to the original String , the original String is changed to new one. why is it so?
No, actually what happens is that the changes you made are foisted on a new String which is created. See the sample code below:
If trim() really trimmed the String of s1 and returned the same object to the String reference s2, then s1 and s2 should be pointing to the same object, which is not the case. Also if we printed out s1 and s2, s1 still has some spaces left over...
Vasantha, So, what you've discovered is that, while you may not be able to change a given object, you can change which object a non-final identifier refers to. AND... Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!
I wonder if at any moment two people are thinking the exact same thought...
Anyway, is there such a distinction as between a String object and a String literal or are they basically one in the same?
Joined: Mar 22, 2002
Sorry, I was just rereading that boy wanna meet girl thread and I suppose my subconscious is playing tricks on me again Anyway.... String literals are pooled, in the sense that object references with the same literal "value" refer to the same object. Please see the sample code below:
More info can be found in the JLS in 3.10.5 String Literals. See also the description of the intern() method in the JDK 1.4 docs. [ September 06, 2002: Message edited by: Anthony Villanueva ]