1. 程式人生 > >String 和 new String

String 和 new String

都是 == 和equal left ali str 對象 int 一個

String s1=”welcome”;String s2= “welcome:;String s3 =new String(”welcome”); //新建對象 指向新的內存 比較內容相同 但是地址不同 System.out.println(s1.equals(s3));System.out.println(s1 ==s3);== 是比較內存地址equals 是比較內容 在s1 和s2比較的時候,他們都引用了同一塊內存地址,所以他們 ==和equals 都是true但是在s1和s3比較的時候, 因為s3是new String ,相當於在內存中新建了一個對象 ,指向了一塊新的內存地址,所以在比較==的時候,引用地址是不同的 ,報false,比較equals的時候是相同的

String 和 new String