Value transfer or reference Transfer

Source: Internet
Author: User

Code Analysis:

Public class ParamTest {// The initial value is 0 protected int num = 0; // The public void change (int I) {I = 5;} is assigned to the method parameter again ;} // assign public void change (ParamTest t) {ParamTest tmp = new ParamTest (); tmp to the method parameter. num = 9; t = tmp;} // change the value of the method parameter public void add (int I) {I + = 10 ;} // change the public void add (ParamTest pt) {pt. num + = 20;} public static void main (String [] args) {ParamTest t = new ParamTest (); System. out. println ("parameter -- Basic Type"); System. out. println ("Original Value:" + t. num); // re-assign t for the basic type parameter. change (t. num); System. out. println ("after assignment:" + t. num); // value t for the referenced parameter. change (t); System. out. println ("after calculation:" + t. num); System. out. println (); t = new ParamTest (); System. out. println ("parameter -- reference type"); System. out. println ("Original Value:" + t. num); // change the value of the basic type parameter t. add (t. num); System. out. println ("after being referenced:" + t. num); // change the attribute value t of the object to which the reference type parameter points. add (t); System. out. println ("after modifying attributes:" + t. num );}}

 

The output result is as follows: parameter -- original value of the basic type: 0 after Value assignment: 0 after calculation: 0 parameter -- Original Value of the reference type: 0 after reference: 0 after attribute modification: 20. Conclusion: 1. when the basic type and basic type variables are passed as parameters to the method, they are passed as values. In a method object, you cannot assign a value to the original variable or change its value. 2. When an object or referenced variable is passed to a method as a parameter, the original variable cannot be assigned a value in the method object, but the attributes of the object to which it points can be changed. It does not matter whether it is a value transfer or a reference transfer. It is important to know what will happen in the method body when a reference is passed as a parameter to a method.

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.