The commonly agreed answer to this interview question is that two objects are created by the code. But I don\'t think so; I wrote some code to confirm.
publi
java.lang.String
overrides the hashCode()
method so that the value depends on the content of the string.
As a result, hashCode()
does not tell you anything about the number of instances. It may be the same string or may be another instance with no single byte shared. Same about equals()
. This explains your output.
Use System.identityHashCode(..) for this kind of research.
And may the source be with you.