JAVA中的堆與棧
更新時間: 2007-05-10 13:44:13來源: 粵嵌教育瀏覽量:937
1. 棧(stack)與堆(heap)都是Java用來在Ram中存放數據的地方,本文對其進行比較。
2. 棧的優勢是,存取速度比堆要快,僅次于直接位于CPU中的寄存器。但缺點是,存在棧中的數據大小與生存期必須是確定的,缺乏靈活性。另外,棧數據可以共享,詳見第3點。堆的優勢是可以動態地分配內存大小,生存期也不必事先告訴編譯器,Java的垃圾收集器會自動收走這些不再使用的數據。但缺點是,由于要在運行時動態分配內存,存取速度較慢。
3. Java中的數據類型有兩種。基本類型(primitive types), 共有8種,即int, short, long, byte, float, double, boolean, char。存在于棧中。另一種是包裝類數據,如Integer, String, Double等將相應的基本數據類型包裝起來的類。這些類數據全部存在于堆中.
String str = "abc"; 和String str = new String("abc"); 和char[] c = {'a','b','c'}; String str=new String(c); 都采用堆存儲
String str = "abc"; 在棧中如果沒有存放值為"abc"的地址,等同于:
String temp=new String("abc");
String str=temp;
關于String str = "abc"的內部工作。Java內部將此語句轉化為以下幾個步驟:
(1)先定義一個名為str的對String類的對象引用變量:String str;
(2)在棧中查找有沒有存放值為"abc"的地址,如果沒有,則開辟一個存放字面值為"abc"的地址,接著創建一個新的String類的對象o,并將o的字符串值指向這個地址,而且在棧中這個地址旁邊記下這個引用的對象o。如果已經有了值為"abc"的地址,則查找對象o,并返回o的地址。
(3)將str指向對象o的地址。
使用String str = "abc";的方式,可以在一定程度上提高程序的運行速度,因為JVM會自動根據棧中數據的實際情況來決定是否有必要創建新對象。而對于String str = new String("abc");的代碼,則一概在堆中創建新對象,而不管其字符串值是否相等,是否有必要創建新對象,從而加重了程序的負擔。
char[] c = {'a','b','c'}; String str=new String(c); 等同于:
String str = new String('a'+'b'+'c');
====================================================================================
1.
class Test
{
public static void main(String[] args)
{
String s = "123";
}
}
Runtime Heap Summary: Test
==========================
Runtime Instance List
---------------------
Package Class Count Cumulative Count Memory Cumulative Memory
------- ----- ----- ---------------- ------ -----------------
Total 2 (100.0%) 2 (100.0%) 48 (100.0%) 48 (100.0%)
java.lang String 1 (50.0%) 1 (50.0%) 24 (50.0%) 24 (50.0%)
char[ ] 1 (50.0%) 1 (50.0%) 24 (50.0%) 24 (50.0%)
Report Date: 2007-4-26 15:27:49
結論:String s = "123",會創建一個"123"字符數組和一個String對象。
2.
class Test
{
public static void main(String[] args)
{
String s = new String("123");
}
}
Runtime Heap Summary: Test
==========================
Runtime Instance List
---------------------
Package Class Count Cumulative Count Memory Cumulative Memory
------- ----- ----- ---------------- ------ -----------------
Total 3 (100.0%) 3 (100.0%) 72 (100.0%) 72 (100.0%)
java.lang String 2 (66.7%) 2 (66.7%) 48 (66.7%) 48 (66.7%)
char[ ] 1 (33.3%) 1 (33.3%) 24 (33.3%) 24 (33.3%)
Report Date: 2007-4-26 15:29:07
結論:String s = new String("123"); 根據上面的測試可以看出,"123"創建了一個數組,一個String對象,而new String()又根據"123"對象作為參數,重新生成了一個新的String對象,此對象被s變量引用。
3.
class Test
{
public static void main(String[] args)
{
String s1 = "123";
String s2 = "123";
if (s1 == s2) {
System.out.println("s1==s2");
} else {
System.out.println("s1!=s2");
}
}
}
輸出結果:s1==s2
4.
class Test
{
public static void main(String[] args)
{
String s1 = new String("123");
String s2 = new String("123");
if (s1 == s2) {
System.out.println("s1==s2");
} else {
System.out.println("s1!=s2");
}
}
}
結果:s1!=s2
5.
class Test
{
public static void main(String[] args)
{
String s1 = new String("123");
String s2 = new String("123");
}
}
Runtime Heap Summary: Test
==========================
Runtime Instance List
---------------------
Package Class Count Cumulative Count Memory Cumulative Memory
------- ----- ----- ---------------- ------ -----------------
Total 4 (100.0%) 4 (100.0%) 96 (100.0%) 96 (100.0%)
java.lang String 3 (75.0%) 3 (75.0%) 72 (75.0%) 72 (75.0%)
char[ ] 1 (25.0%) 1 (25.0%) 24 (25.0%) 24 (25.0%)
Report Date: 2007-4-26 15:41:50
結論:相同字符串常量,即使在不同語句中被引用,其內存是共用的,"123"只生成一個字符數據和一個
String對象,兩個new String()分別生成了一個對象。
6.
class Test
{
public static void main(String[] args)
{
String s1 = new String("123");
String s2 = new String("1234");
}
}
Runtime Heap Summary: Test
==========================
Runtime Instance List
---------------------
Package Class Count Cumulative Count Memory Cumulative Memory
------- ----- ----- ---------------- ------ -----------------
Total 6 (100.0%) 6 (100.0%) 144 (100.0%) 144 (100.0%)
java.lang String 4 (66.7%) 4 (66.7%) 96 (66.7%) 96 (66.7%)
char[ ] 2 (33.3%) 2 (33.3%) 48 (33.3%) 48 (33.3%)
Report Date: 2007-4-26 15:43:45
結論:"123"和"1234"分別生成了各自的字符數組和String對象。兩個new String()分別創建一個String對象。