java中string記憶體的相關知識點

來源:互聯網
上載者:User

標籤:nec   dpx   goto   nfc   pes   cal   udf   class   ddd   

(一):區別java記憶體中堆和棧:1.棧:資料可以共用,存放基礎資料型別 (Elementary Data Type)和對象的引用,其中對象存放在堆中,對象的引用存放在棧中; 當在一段代碼塊定義一個變數時,就在棧中 為這個變數分配記憶體空間,當該變數退出該範圍後,Java會自動釋放掉為該變數所分配的記憶體空間,該記憶體空間可以立即被另作他用。2.堆:存放用new產生的資料,其中清理垃圾採用記憶體回收機制。 在堆中產生了一個數組或對象後,還可以 在棧中定義一個特殊的變數,讓棧中這個變數的取值等於數組或對象在堆記憶體中的首地址,棧中的這個變數就成了數組或對象的引用變數。  數組和對象在沒有引用變數指向它的時候,才變為垃圾,不能在被使用,但仍 然佔據記憶體空間不放,在隨後的一個不確定的時間被記憶體回收行程收走(釋放掉)。 3.相關代碼:
package zdbString;                                                                           public class Test {                                                                             /*                                                                                            * 這個源檔案主要是測試java中string的記憶體原理、運算子“==”和函數equals()的區別                                          * 總結:(1)引用str1被放在棧中,字串常量hello放在常量池中,引用str1指向常量池中的hello;而引用str3被存放在棧中,同時在堆中開闢一塊新的記憶體存放string對象。(2)                                                                                       * 相關串連網址:http://jingyan.baidu.com/article/8275fc869a070346a03cf6f4.html                      */                                                                                         public static void main(String[] args) {                                                         String str1 = "hello";                                                                       String str2 = "hello";                                                                       String str3 = new String("hello");                                                           String str4 = new String("hello");                                                           System.out.println("str1==str2==="+(str1==str2));//true                                     System.out.println("str1.equals(str2)==="+(str1.equals(str2)));//true                       System.out.println("str3==str4==="+(str3==str4));//false                                     System.out.println("str3.equals(str4)==="+(str3.equals(str4)));//true                       System.out.println("str1==str3==="+(str1==str3));//false                                     System.out.println("str1.equals(str3)==="+(str1.equals(str3)));//true                   }                                                                                       }                                                                                           
View Code

 4.相關記憶體配置圖:

 

java中string記憶體的相關知識點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.