public class Test{
static String s1 = "abc";
public static void main(String[] args) {
String s2 = "abc";
String s3 = new String("abc");
String s4 = "abc";
System.out.println("s1==s2: "+(s1==s2));
System.out.println("s1==s3: "+(s1==s3));
System.out.println("s2==s3: "+(s2==s3));
System.out.println("s2==s4: "+(s2==s4));
System.out.println("s1equals(s2)------"+s1.equals(s2));
System.out.println("s1equals(s3)------"+s1.equals(s3));
System.out.println("s2equals(s3)------"+s2.equals(s3));
System.out.println("s2equals(s4)------"+s2.equals(s4));
}
}
为什么s1==s2 结果为true
static是在静态代码块中,而String字符串是在常量池中,==在比较的时候是比较的地址
难道说静态static和String在内存中存储在一个区吗 |
|