为什么这个代码将打印
真正
真正
public class Test {
public static void main(String[] args){
String st1 = "abc";
String st2 = "abc";
Integer k1 = 100;
Integer k2 = 100;
System.out.println(st1 == st2);
System.out.println(k1 == k2);
}
}
要比较对象,我们使用方法equals()。但为什么这样可以呢?
**你期望什么?为什么? –
由于字符串文字和小整数被禁用。 –
还有:http://stackoverflow.com/questions/3281448/strings-in-java-equals-vs。然后:http://stackoverflow.com/questions/10673632/javahow-come-2-literalsthe-same-contents-in-2-classes-use-the-same-interned-s –