String 深浅拷贝的测试---有待继续测试

时间:2023-02-03 16:36:54

public class TestString {

    void test1() {
// TODO Auto-generated method stub
String str = new String();
str = "12345"; String STR = "12345";
System.out.println(str == STR); String str_1 = str; String str_2 = null;
str_2 = str; String str_3 = new String();
str_3 = str; String str_4 = new String(str); System.out.println("str:" + str); System.out.println("str_1:" + str_1);
System.out.println("str == str_1:" + str == str_1); System.out.println("str_2:" + str_2);
System.out.println("str == str_2:" + str == str_2); System.out.println("str_3:" + str_3);
System.out.println("str == str_3:" + str == str_3); System.out.println("str_4:" + str_4);
System.out.println("str == str_4:" + str == str_4); str = "2345678"; System.out.println("str:" + str); System.out.println("str_1:" + str_1);
System.out.println("str == str_1:" + str == str_1); System.out.println("str_2:" + str_2);
System.out.println("str == str_2:" + str == str_2); System.out.println("str_3:" + str_3);
System.out.println("str == str_3:" + str == str_3); System.out.println("str_4:" + str_4);
System.out.println("str == str_4:" + str == str_4);
} static void test2(){//false
String str = new String("123");
String str_2 = new String("123");
System.out.println( str == str_2);
} static void test3(){//false
String str = "123";
String str_2 = new String("123");
System.out.println( str == str_2);
} static void test4(){//true
String str = "123";
String str_2 = "123";
System.out.println( str == str_2);
} public static void main(String[] args) {
test2();
test3();
test4(); } }

 

总结:new 一定是深拷贝,好要继续实验