posted 21 years ago
OK, that's a much better question.
Each time you create a "new Integer", you get a different Integer object. When you do "i==i1", you are comparing the object references, and these objects are not the same, so it's false.
The compiler treats String literals specially. Every instance of the string literal "foo", no matter where it appears, is the same String object. So "s==s1" is true.
This is true only of String literals, not Strings in general. If you had
String s = "abc";
String s1 = s.charAt(0) + "bc";
then s==s1 will be false. The two strings have the same contents but are different objects.
[ October 31, 2002: Message edited by: Ron Newman ]
Ron Newman - SCJP 1.2 (100%, 7 August 2002)