有很多关于字符串不变性的问题,但我还没有找到为什么会发生以下情况的答案:
1) "TEST".equals("TEST") // TRUE obviously
2) "TEST" == "TEST" // Also TRUE since String's are immutable
3) "TEST" == "T" + "E" + "S" + "T" // TRUE, but why? Due to compiler optimalization?
4) "TEST" == new String("TEST"); // FALSE, because you explicitly request a new String?
如有必要,谁能纠正我并进一步详细解释 3 和 4?非常感谢!