String两种实例化区别
public class StringTest {
public static void main(String[] args) {
String s1 = new String("abc");//构造器实例化,声明在堆
String s2 = new String("abc");
String s3 ="abc";//字面量实例化,声明在常量池
String s4 ="abc";
System.out.println(s1==s2);//false
System.out.println(s3==s4);//true
String s5 = s3+s4;//变量声明在栈
String s6 = "abcabc"//常量池
System.out.println(s5==s6);//false
String s7 = "abc"+"abc";//常量池
System.out.println(s7==s6);//true
System.out.println(s7==s5);//false
}
}

浙公网安备 33010602011771号