Study dolly's code and you can see why Strings are immutable. As to force error to come out by changing existing String object without compiler automatically creating new one.... hmmm no idea. Perhaps java designer designed it this way where it's hard for you to create errors. So people don't keep crashing programs.
there are two things created. there is the actual String object, that lives on heap. then, there is the reference "s" that points to that object in memory.
My favorite analogy is a house, and a piece of paper. the house is the object. the paper is your reference.
you build the house, and write down the address on the slip of paper.
You can't change - physically alter - the house, but you can erase one address and put another one on the paper. The house is immutable, the paper is not.
so with what exact code are changing the value of a String object?
I don't understand your question here. you CANNOT change the value of a string once it is created. it's not a question of the compiler saying "Nope!!!". There is simply no method that exists that will change the string.
all the things that you might THINK change the string don't - they actually create NEW strings, leaving the old ones untouched.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
so with what exact code are changing the value of a String object? (lead to compile or runtime whatever error?)
As you see, the original String object "Hello" is not changed by the concat() method. Just a new String "Hello World" is created and return to you. String object can't be changed by any method, that's what immutable mean.