heyas!
firstly, strings are immutable. this is important for
scjp. it basically means that for resource purposes, any strings you create are put in a
string pool. if you try to create a string with identical chars to an existing string, the jvm will just create a new referece, but point to the existing, identical string in the pool
firstly string 's' object is created and given 'hai' as its value
sencondly it is placed in the string pool
so we have STRING POOL: 'Hai'
references: s => Hai
when we call string str ... blah we then create a new reference, called str, but it points to the existing string; Hai
so we have STRING POOL: 'Hai'
references: s => Hai
str => Hai
where => means points to
...........
>>Also, in this code
simply put, two references are created s1 and s2. in the string pool two objects are created, Hai and Hello
String Pool: 'Hai' , 'Hello'
References: s1 => Hai
s2 => Hello
therefore if i were to create String example = "Hello"; we would also have
example referenceing to an existing 'Hello' string in the pool
example => Hello
hope that helps!
-twans