Q:

Java how are all String objects immutable?

I'm sorry if this is an obvious question as I am a beginner in Java.
String objects are immutable, meaning that they can't be changed.
So if you instantiate:
String s = "hello";
and then change the contents of s,
s = "goodbye";
does this mean that a new String object is created, also called s? Is the value of the original variable s being changed? Or is its reference being changed? Thank you in advance as the concept of string immutability is confusing me.

A:

When you create a 
String s = "hello";

after lookin in string pool, java creates a new string in string pool and returns a reference of that string to s. 
Now when you change the contents of s as
s = "goodbye"; 

again if the goodbye is not in string pool, it will create a new string in string pool and returns a reference of newly created string.
So the answer your question is strings are immutable.
If you have many operations to perform and your string content is going to be changed frequently, you can use string builder or string buffer, these classes are mutable. 

