Java String a=new String("ABC")的建立

來源:互聯網
上載者:User

標籤:引入   word   拼接   根據   詳解   img   gravity   string類   print   

題目

String s = new String(“hello”)和String s = “hello”;的區別?

區別

String s = new String(“hello”)會建立2(1)個對象,String s = “hello”建立1(0)個對象。 
註:當字串常量池中有對象hello時括弧內成立!

引入

==與equals()的區別:

  1. ==:比較參考型別比較的是地址值是否相同
  2. equals:比較參考型別預設也是比較地址值是否相同,而String類重寫了equals()方法,比較的是內容是否相同。
Demo
public class StringDemo2 {    public static void main(String[] args) {        String s1 = new String("hello");        String s2 = "hello";        System.out.println(s1 == s2);// false        System.out.println(s1.equals(s2));// true    }}**運行結果:**> false > true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
記憶體配置圖

代碼詳解
  1. 首先,通過main()方法進棧。
  2. 然後再棧中定義一個對象s1,去堆中開闢一個記憶體空間,將記憶體空間的引用賦值給s1,“hello”是常量,然後去字串常量池 查看是否有hello字串對象,沒有的話分配一個空間存放hello,並且將其空間地址存入堆中new出來的空間中。
  3. 在棧中定義一個對象s2,然後去字串常量池中查看是否有”hello”字串對象,有,直接把”hello”的地址賦值給s2.
  4. 即s1中存的是堆中分配的空間,堆中分配的空間中存的是字串常量池中分配空間存放”hello”的空間的地址值。而s2中之間存的是字串常量池中分配空間存放”hello”的空間的地址值。
  5. 由於s1與s2中存放的地址不同,所以輸出false。因為,類String重寫了equals()方法,它比較的是參考型別的 的值是否相等,所以輸出true。即結果為false、true。
Demo1
public class StringDemo1 {    public static void main(String[] args) {        String s1 = new String("hello");        String s2 = new String("hello");        System.out.println(s1 == s2);// false        System.out.println(s1.equals(s2));// true        String s3 = new String("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    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Demo1詳解

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

  1. s1、s2:二者均為new出來的,各自在堆中分配有空間,並各自將記憶體位址賦值給s1、s2。空間地址不同,==比較為false。但是各自在堆中空間中儲存的值均為在字串常量池中的同一個對象的地址。根據Demo處的圖即解釋不難理解。
  2. s3、s4同上Demo出解釋。
  3. s5、s6都是在常量池中取值,二者都指向常量池中同一對象,其地址值相同,所以結果為true。
Demo2
public class StringDemo4 {    public static void main(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    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Demo2詳解

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

  1. s1與s2相加是先在字串常量池中開一個空間,然後拼接,這個空間的地址就是s1與s2拼接後的地址。與s3的地址不同,所以輸出為false。
  2. s3與”hello”+”world”作比較,”hello”+”world”先拼接成”helloworld”,然後再去字串常量池中找是否有”helloworld”,有,所以和s3共用一個字串對象,則為true。
總結
    1. String s = new String(“hello”)會建立2(1)個對象,String s = “hello”建立1(0)個對象。 
      註:當字串常量池中有對象hello時括弧內成立!
    2. 字串如果是變數相加,先開空間,在拼接。
    3. 字串如果是常量相加,是先加,然後在常量池找,如果有就直接返回,否則,就建立。

Java String a=new String("ABC")的建立

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.