Java堆、棧和常量池

quguiliang 14年前發布 | 800 次閱讀

  1.寄存器:最快的存儲, 由編譯器根據需求進行分配,我們在程序中無法控制.

  2. 棧:存放基本類型的變量數據和對象的引用,但對象本身不存放在棧中,而是存放在堆(new 出來的對象)或者常量池中(字符串常量對象存放在常量池中。)

  3. 堆:存放所有new出來的對象。

  4. 靜態域:存放靜態成員(static定義的)

  5. 常量池:存放字符串常量和基本類型常量(public static final)。

  6. RAM存儲:硬盤等永久存儲空間

  這里我們主要關心棧,堆和常量池,對于棧和常量池中的對象可以共享,對于堆中的對象不可以共享。棧中的數據大小和生命周期是可以確定的,當沒有引用指向數據時,這個數據就會消失。堆中的對象的由垃圾回收器負責回收,因此大小和生命周期不需要確定,具有很大的靈活性。

  對于字符串:其對象的引用都是存儲在棧中的,如果是編譯期已經創建好(直接用雙引號定義的)的就存儲在常量池中,如果是運行期(new出來的)才能確定的就存儲在堆中。對于equals相等的字符串,在常量池中永遠只有一份,在堆中有多份。

  如以下代碼:

  Java代碼

源代碼復制打印

  1. String s2 = "china";   
  2.   
  3. String s3 = "china";   
  4.   
  5. String ss1 = new String("china");   
  6.   
  7. String ss2 = new String("china");   
  8.   
  9. String ss3 = new String("china");  

String s2 = "china";

 

String s3 = "china";

 

String ss1 = new String("china");

 

String ss2 = new String("china");

 

String ss3 = new String("china");

 

  

  對于基礎類型的變量和常量:變量和引用存儲在棧中,常量存儲在常量池中。

  如以下代碼:

  Java代碼

源代碼復制打印

  1. int i1 = 9;   
  2.   
  3. int i2 = 9;   
  4.   
  5. int i3 = 9;   
  6.   
  7. public static final int INT1 = 9;   
  8.   
  9. public static final int INT2 = 9;   
  10.   
  11. public static final int INT3 = 9;  

int i1 = 9;

 

int i2 = 9;

 

int i3 = 9;

 

public static final int INT1 = 9;

 

public static final int INT2 = 9;

 

public static final int INT3 = 9;

 

  

  對于成員變量和局部變量:成員變量就是方法外部,類的內部定義的變量;局部變量就是方法或語句塊內部定義的變量。局部變量必須初始化。

形式參數是局部變量,局部變量的數據存在于棧內存中。棧內存中的局部變量隨著方法的消失而消失。

  成員變量存儲在堆中的對象里面,由垃圾回收器負責回收。

  如以下代碼:

  Java代碼

源代碼復制打印

  1. class BirthDate {   
  2.     private int day;   
  3.     private int month;   
  4.     private int year;   
  5.        
  6.     public BirthDate(int d, int m, int y) {   
  7.     day = d;   
  8.     month = m;   
  9.     year = y;   
  10.     }   
  11.     //省略get,set方法………   
  12. }  

class BirthDate {

  private int day;

  private int month;

  private int year;

 

  public BirthDate(int d, int m, int y) {

  day = d;

  month = m;

  year = y;

  }

  //省略get,set方法………

}

源代碼復制打印

  1. public class Test{   
  2.   
  3.     public static void main(String args[]){   
  4.         int date = 9;   
  5.         Test test = new Test();   
  6.         test.change(date);   
  7.         BirthDate d1= new BirthDate(7,7,1970);   
  8.     }   
  9.   
  10.     public void change1(int i){   
  11.     i = 1234;   
  12.     }   
  13. }  

public class Test{

 

public static void main(String args[]){

   int date = 9;

   Test test = new Test();

   test.change(date);

   BirthDate d1= new BirthDate(7,7,1970);

}

 

public void change1(int i){

i = 1234;

}

}

 

  

  對于以上這段代碼,date為局部變量,i,d,m,y都是形參為局部變量,daymonthyear為成員變量。下面分析一下代碼執行時候的變化:

  1. main方法開始執行:int date = 9;

  date局部變量,基礎類型,引用和值都存在棧中。

  2. Test test = new Test();

  test為對象引用,存在棧中,對象(new Test())存在堆中。

  3. test.change(date);

  i為局部變量,引用和值存在棧中。當方法change執行完成后,i就會從棧中消失。

  4. BirthDate d1= new BirthDate(7,7,1970);

  d1為對象引用,存在棧中,對象(new BirthDate())存在堆中,其中dmy為局部變量存儲在棧中,且它們的類型為基礎類型,因此它們的數據也存儲在棧中。day,month,year為成員變量,它們存儲在堆中(new BirthDate()里面)。當BirthDate構造方法執行完之后,d,m,y將從棧中消失。

  5.main方法執行完之后,date變量,testd1引用將從棧中消失,new Test(),new BirthDate()將等待垃圾回收。

 

 本文由用戶 quguiliang 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!