Passing the parameters of the Java method

Source: Internet
Author: User

 

Passing the parameters of the Java method

Basic Type

 

The parameter passing of Java methods is sometimes not easy to understand, and some analysis is carried out based on simple graphics. For more information, see <Java Employment Training tutorial>.

This article first looks at the transmission of parameters of the basic type.

The following code is used as an example:

Public class passvalue {

Public staticvoid main (string [] ARGs ){

Int x = 5;

Change (X );

System. Out. println (X );

}

Public staticvoid change (int x ){

X = 3;

}

}

The parameter transfer process during method Runtime is to first open up a space for the variable of the defined type in the method area, and the parameter variable (that is, the local variable in the method) points to this space. See Figure 1_1.

Then, assign the value of the real parameter to this local variable. In this case, the local variable X in the method is assigned to 5. See Figure 1_2.

 

At this point, the transfer of the call method change parameter is complete. Check the code running status. In the method, the value of X of the local variable is 3, but the value of X outside the method is 5. See Figure 1_3.

The change method ends. The memory occupied by X in the change method is released and the program returns the result. The variable X in the main method is still 5 and has not changed. See Figure 1_4.

At this point, the entire program is executed.

Through the image, you can clearly see the variable transfer process.

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.