Test People's Java memory load surface questions

Source: Internet
Author: User

The source code is as follows, finding the result

 Public classMemoryanalyse { Public Static intK = 0;  Public StaticMemoryanalyse T1 =NewMemoryanalyse ("T1");  Public StaticMemoryanalyse t2 =NewMemoryanalyse ("T2");  Public Static inti = print ("I");  Public Static intj = Print ("J");  Public Static intn = 99; {Print ("Constructor Code"); }        Static{print ("Static Code"); }     Public Static intprint (String s) {System.out.println ("I=" + i + "" + S + "k=" + K + "n=" +N+ "j=" +j); ++i; ++K; ++N; returni; }     PublicMemoryanalyse (String string) {print (string); }     Public Static voidMain (string[] args)throwsclassnotfoundexception {Memoryanalyse d=NewMemoryanalyse ("T");    }}

But the result is this

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=0  I=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

Was it surprising that the result was so complicated. OK, let's analyze it, before we analyze it, we'll get to the basics

When code blocks and static code blocks run the problem:

  Code blocks run when an object is created
Static code blocks run when the class loads

Everyone knows that static is not an object of a class, which means that the static modifier exists there when the class is loaded into the method area. So the memory procedure in the method area is as follows

1. When the class is loaded completely into the method area, allall variables are not instantiated.

2.Instantiate parameter T1

At this point, code blocks are executed first because they are executed when the object is created and executed before the constructor

{print ("constructor Code");}

Because all of the variables are default values at this point, the print result after execution is

I=0 Constructor Code k=0 n=0 j=0

At this point, the value of i,n,k has been added to a value of 1

Then instantiate the call constructor

 Public Memoryanalyse (String string) {   //here string is T1        print (string);}

The constructor call results are as follows

  I=1 T1 k=1 n=1 j=0
At this point, the value of i,n,k has been added to a value of 2

3. Instantiate parameter T2

Execute the code block before the constructor, as in the first step

{Print ("constructor code");}

I=2 Constructor Code k=2 n=2 j=0

At this point, the value of i,n,k has been added to a value of 3

Then instantiate the call constructor

Public Memoryanalyse (string string) {   //here string is T2        print (string);}

The constructor call results are as follows

I=3 T2 k=3 n=3 j=0

At this point, the value of i,n,k has been added to a value of 4

4. Initialize parameter I

The print ("I") function is called directly here to get the result

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

At this point the I,k,j value is 5, note that the value of I is not by adding one to 5, but by the return value of the function assigned to I

5. Initialization Parameters J

  

Here, as in the previous step, execute print ("J"), then assign the return value of the function to J and print the result as

  I=5 J k=5 N=5 j=0

At this point, the value of J is 6.

In this case, the internal parameter changes of the class load are completed, and we can call them in the way of the load class.

     Public Static void throws classnotfoundexception {        //Memoryanalyse d = new Memoryanalyse ("T");        Class.forName ("Memoryanalyse");    }

The Class.forName (class name string) is the manual loading of the class to the method area, resulting in a

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 //This line occurs because the static snippet is executed when the class loads. n=99 is because the class is loaded, and the initial value of n is 99 .

Then change to our previous demo

 Public Static void throws classnotfoundexception {        new Memoryanalyse ("T");         // class.forname ("Memoryanalyse");    }

Execution results are

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 //block execution
i=8 T k=8 n=101 j=6 //constructor execution

The last two lines are very simple, one is the code block, the other is the constructor

So an analysis is not a lot simpler

Test People's Java memory load surface questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.