Maybe I am just crazy, or maybe I forgot the way things should work. But I was chasing down a bug (while learning some Groovy) and found that the following assertion fails. I guess I thought that when using a string literal, a new instance of String would be created for it. Am I wrong?
String one = 'one'
String two = 'one'
// I thought this should fail, but it doesn't
assert one.is(two) == false
one = new String("one")
two = new String("one")
// This works as expected
assert one.is(two) == false
By using the dump() method, I can see that they are indeed the same instance of String. Any help or guidance would be appreciated.
Thanks!
When you use String literals, Java allows them to be interned, so you get the same instance..
The section 2.3 in the VM spec mentions this.
Groovy also follows this rule