標籤: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記憶體的相關知識點