很考驗人的java記憶體載入面試題

來源:互聯網
上載者:User

標籤:cto   初始化   memory   return   code   直接   public   logs   建立   

原始碼如下,求結果

public class MemoryAnalyse {    public static int k = 0;    public static MemoryAnalyse t1 = new MemoryAnalyse("t1");    public static MemoryAnalyse t2 = new MemoryAnalyse("t2");    public static int i = print("i");    public static int j = print("j");    public static int n = 99;        {        print("constructor code");    }        static {        print("static code");    }    public static int print(String s) {        System.out.println("i=" + i + "   " + s + "  k=" + k + "  n=" + n                + "   j=" + j);        ++i;        ++k;        ++n;        return i;    }    public MemoryAnalyse(String string) {        print(string);    }    public static void main(String[] args) throws ClassNotFoundException {        MemoryAnalyse d = new MemoryAnalyse("T");    }}

然而結果是這個

i=0   constructor code  k=0  n=0   j=0i=1   t1  k=1  n=1   j=0i=2   constructor code  k=2  n=2   j=0i=3   t2  k=3  n=3   j=0i=4   i  k=4  n=4   j=0i=5   j  k=5  n=5   j=0i=6   static code  k=6  n=99   j=6i=7   constructor code  k=7  n=100   j=6i=8   T  k=8  n=101   j=6

有沒有很驚訝,結果竟然這麼複雜.好,下面我們分析一下,在分析之前,先普及下不怎麼用的基礎知識

代碼塊和靜態代碼塊何時運行問題:

  代碼塊在建立對象時運行
  靜態代碼塊在類載入時運行

大家都知道static是屬於類的並非對象,也就是說static修飾的東西都會在class載入到方法區時就存在在那裡.所以方法區中類載入時記憶體過程如下

1.當類剛載入時會全部載入到方法區時,此時所有變數全部未執行個體化.

2.執行個體化參數t1

此時因為代碼塊在建立對象時執行,且在建構函式之前執行,所以先執行代碼塊

{print("constructor code");}

  因為此時所有的變數都為預設值,所以執行後列印結果為

i=0   constructor code  k=0  n=0   j=0

此時i,n,k的值都已經自加一,值為1

然後執行個體化調用建構函式

public MemoryAnalyse(String string) {   //這裡string為t1        print(string);}

  建構函式調用結果如下

  i=1   t1  k=1  n=1   j=0
  此時i,n,k的值都已經自加一,值為2

3.執行個體化參數t2

和第一步一樣在建構函式之前執行代碼塊

{print("constructor code");}

i=2   constructor code  k=2  n=2   j=0

此時i,n,k的值都已經自加一,值為3

然後執行個體化調用建構函式

public MemoryAnalyse(String string) {   //這裡string為t2        print(string);}

  建構函式調用結果如下

 

i=3   t2  k=3  n=3   j=0

此時i,n,k的值都已經自加一,值為4

4.初始化參數i

  這裡直接調用print("i")函數,得到結果為

  i=4   i  k=4  n=4   j=0

  此時i,k,j值為5,注意i的值不是通過自加一變成5的,而是通過函數的傳回值賦給i的

  5.初始化參數j

  

  這裡和上一步一樣,執行print("j"),然後把函數的傳回值賦給j,列印結果為

  i=5   j  k=5  n=5   j=0

  此時j的值已經為6

到這裡類載入的內部參數變化就完成了,我們可以用載入類的方式調用一下

    public static void main(String[] args) throws ClassNotFoundException {        //MemoryAnalyse d = new MemoryAnalyse("T");        Class.forName("MemoryAnalyse");    }

Class.forName(類名字串)是手動載入類到方法區,得到結果為

i=0   constructor code  k=0  n=0   j=0
i=1   t1  k=1  n=1   j=0
i=2   constructor code  k=2  n=2   j=0
i=3   t2  k=3  n=3   j=0
i=4   i  k=4  n=4   j=0
i=5   j  k=5  n=5   j=0
i=6   static code  k=6  n=99   j=6  //這行的出現是因為static程式碼片段在類載入時執行.n=99是因為類載入了,n的初值為99把之前的值覆蓋掉了

然後改為我們之前的demo

public static void main(String[] args) throws ClassNotFoundException {        MemoryAnalyse d = new MemoryAnalyse("T");        //Class.forName("MemoryAnalyse");    }

執行結果為

i=0   constructor code  k=0  n=0   j=0
i=1   t1  k=1  n=1   j=0
i=2   constructor code  k=2  n=2   j=0
i=3   t2  k=3  n=3   j=0
i=4   i  k=4  n=4   j=0
i=5   j  k=5  n=5   j=0
i=6   static code  k=6  n=99   j=6
i=7   constructor code  k=7  n=100   j=6    //代碼塊執行
i=8   T  k=8  n=101   j=6     //建構函式執行

最後兩行的出現就很簡單了,一個是代碼塊的,一個是建構函式的

這麼一分析是不是簡單了很多

很考驗人的java記憶體載入面試題

聯繫我們

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