Discussion on the problems related to the transfer of method parameters in Java

Source: Internet
Author: User

We know that there are two purposes for defining variables in Java:

1. Prevent the garbage collection mechanism from being recycled, after all, if the ' code ' is not explicitly pointing to real physical memory, it is likely to be garbage collected by the Java garbage collection mechanism.

2. Easy to reference and easy to handle.

1  PackageTESTP1;2 3  Public classTest01 {4      Public Static voidReplace1 (inta) {5A = 10;6     }7      Public Static intReplace2 (inta) {8A = 10;9         returnA;Ten     } One      Public Static voidReplace3 (String s) { As = "a"; -     } -      Public Staticstring Replace4 (string s) { thes = "a"; -         returns; -     } -      +  Public Static voidMain (string[] args) { -     intA = 0; + Replace1 (a); ASystem.out.println ("in replace1:a=" +a); atA =Replace2 (a); -System.out.println ("in replace2:a=" +a); -String s = "Make"; - Replace3 (s); -System.out.println ("in replace3:s=" +s); -s=Replace4 (s); inSystem.out.println ("in replace4:s=" +s); - } to}

Local variables and external variables

In the Replace1 of the above code, the parameter A is the basic data type, which points to the physical memory where the number 10 is located, and does not change the physical address that the external variable a points to when the method is called. The output does not change the value of the variable, but if we call the method of the int return type REPLACE2 we find that its value is changed because we assign the physical address of the internal variable to the external variable A, in the method Replace3 of the reference data type that returns the value void, The result of the method is that the form parameter refers to the object A, also only within the method of the function, out of the method, the variable is the original reference object, in the return type is a string type, we also let the external variable point to the physical memory of object A, the value of course will change.

Discussion on the problems related to the transfer of method parameters in Java

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.