How to prove java string immutability via code? Asked in recent interview.
Interviewer asked me to prove that Java Strings are immutable.
I couldn't bring myself to simply concatenate a String and print it, because concat() returns a new String rather than modifying the original.
Then I thought hash codes might help, but the interviewer stopped me there, pointing out that different Strings can have different hash codes anyway.
If we could access Java's underlying memory directly, I could prove that the original String object remains unchanged. But without going that deep, is there any other convincing way to demonstrate String immutability?
What would you have answered?