筆試題:String s = new String(“hello”)和String s = “hello”;的區(qū)別?

區(qū)別

String s = new String(“hello”)會創(chuàng)建2(1)個對象,String s = “hello”創(chuàng)建1(0)個對象。

注:當字符串常量池中有對象hello時括號內(nèi)成立!

引入

==與equals()的區(qū)別:

==:比較引用類型比較的是地址值是否相同

equals:比較引用類型默認也是比較地址值是否相同,而String類重寫了equals()方法,比較的是內(nèi)容是否相同。

Demo

publicclassStringDemo2 {publicstaticvoidmain(String[] args) {

? ? ? ? ? ?String s1 =newString("hello");

? ? ? ? ? ?String s2 ="hello";

? ? ? ? ? ?System.out.println(s1 == s2);// false

? ? ? ? ? System.out.println(s1.equals(s2));// true}

}

**運行結(jié)果:**

>false

>true


代碼詳解

首先,通過main()方法進棧。

然后再棧中定義一個對象s1,去堆中開辟一個內(nèi)存空間,將內(nèi)存空間的引用賦值給s1,“hello”是常量,然后去字符串常量池 查看是否有hello字符串對象,沒有的話分配一個空間存放hello,并且將其空間地址存入堆中new出來的空間中。

在棧中定義一個對象s2,然后去字符串常量池中查看是否有”hello”字符串對象,有,直接把”hello”的地址賦值給s2.

即s1中存的是堆中分配的空間,堆中分配的空間中存的是字符串常量池中分配空間存放”hello”的空間的地址值。而s2中之間存的是字符串常量池中分配空間存放”hello”的空間的地址值。

由于s1與s2中存放的地址不同,所以輸出false。因為,類String重寫了equals()方法,它比較的是引用類型的 的值是否相等,所以輸出true。即結(jié)果為false、true。

Demo1

publicclassStringDemo1 {publicstaticvoidmain(String[] args) {

String s1 =newString("hello");

String s2 =newString("hello");

System.out.println(s1 == s2);// false

System.out.println(s1.equals(s2));// true

String s3 =newString("hello");

String s4 ="hello";

System.out.println(s3 == s4);// false

System.out.println(s3.equals(s4));// true

String s5 ="hello";

String s6 ="hello";

System.out.println(s5 == s6);// true

System.out.println(s5.equals(s6));// true}

}

結(jié)果:

Demo1詳解

s1~s6用equals()的比較不解釋,都是比較的值,均為true。以下講解==

s1、s2:二者均為new出來的,各自在堆中分配有空間,并各自將內(nèi)存地址賦值給s1、s2??臻g地址不同,==比較為false。但是各自在堆中空間中保存的值均為在字符串常量池中的同一個對象的地址。根據(jù)Demo處的圖即解釋不難理解。

s3、s4同上Demo出解釋。

s5、s6都是在常量池中取值,二者都指向常量池中同一對象,其地址值相同,所以結(jié)果為true。

Demo2

publicclassStringDemo4 {publicstaticvoidmain(String[] args) {

String s1 ="hello";

String s2 ="world";

String s3 ="helloworld";

System.out.println(s3 == s1 + s2);// false

System.out.println(s3.equals((s1 + s2)));// true

System.out.println(s3 =="hello"+"world");//false

System.out.println(s3.equals("hello"+"world"));// true}

}

結(jié)果:


Demo2詳解

equals()比較方法不解釋,比較值,均相等,均為true。

s1與s2相加是先在字符串常量池中開一個空間,然后拼接,這個空間的地址就是s1與s2拼接后的地址。與s3的地址不同,所以輸出為false。

s3與”hello”+”world”作比較,”hello”+”world”先拼接成”helloworld”,然后再去字符串常量池中找是否有”helloworld”,有,所以和s3共用一個字符串對象,則為true。

總結(jié)

String s = new String(“hello”)會創(chuàng)建2(1)個對象,String s = “hello”創(chuàng)建1(0)個對象。

注:當字符串常量池中有對象hello時括號內(nèi)成立!

字符串如果是變量相加,先開空間,在拼接。

字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否則,就創(chuàng)建。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容