I think this has to do with the fact that java stores the str1 literal ("java is fun") in the string pool.
You then set str2 to point to this literal.
Consequently you make the str1 variable point to another literal ("java is not fun"), but the str2 variable is still referring to the first literal.
No, I don't think the String pool has anything to do with why the two are different. It has to do with assignment, as BG said. You are assigning the two references in that order, so that is what they will contain. The assignment in line 5 is applied before that in line 6 and the two are independent.Why did you expect to have both references pointing to the same String in line 7?
Strings are immutable, you cannot change it.
Your str1 is pointing to one string literal (as Brecht said).
Your str2 is pointing to the same String object.
But when you assign new value to str1, new String object is created in string pool(because the existing object cannot be changed), and reference str1 is automatically "redirected" to point to that new object. You cannot change existing string
vineet walia wrote:Strings are reference type in Java
. . .
java is not fun
Java is fun!
java is not fun
java is not fun
if you create string literal it is stored in string literal pool, so again if you create the same literal object with different reference then no new object is created but instead it will internd to same object as str1 and str2 points to same object, then if change the object value and assgin str1 then it current object pointing that reference still str2 pointing to first object
sohail hussain wrote:. . . if you create string literal it is stored in string literal pool, so again if you create the same literal object . . .
But the OP isn't writing the same String literal twice. It doesn't matter whether you use the String pool or not.
What matters is that the assignment operator takes the value on its right and applies it to the variable on its left. I think OP is getting confused and thinks it takes the contents of the variable on its left and applies that to the expression on its right, but it doesn't.
Please don't simply quote the whole of a preceding post, but only the relevant parts. I have removed some of the quote.
There seems to be a fundamental misunderstanding of how the String Pool is populated. String literals can already be identified at compile time so you can think of the String Pool as a cache of String objects that is populated when classes are loaded. The description "creates a string literal that is interned and then creates another" is not actually what happens. It's more like you have a lookup table whose entries are referenced directly wherever the code uses a string literal.
String objects that are created at runtime can be interned, otherwise, they are created on the heap and are treated like any other dynamically instantiated object.
vineet walia wrote:... but It doesn't look like working.
Going back to OP's original question, it's not that it isn't working; it's working the way it's supposed to work. You're just not having the right expectation.
It seems like you think that by doing this: str2 = str1 you think that whatever you do with str1 will affect str2 so that when you reassign str1 to a different value, str2 will somehow follow along, as if it were attached to str1. That's not how it works at all.
Here's what does happen:
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop