Java self-study notes series: encapsulated Java value transfer call

Source: Internet
Author: User
Tags call by reference

Let's take a look at the following code: package com. ivantian. coreFengZhuang; // call by values // call by address public class Difference {int x = 10; // member variable public static void main (String [] args) {int a = 10; int B = a; System. out. println ("this is a value reference:"); System. out. println ("data before test:"); System. out. println ("the value of a is:" + a + "the value of B is:" + B); a = 20; System. out. println ("tested data:"); System. out. println ("the value of a is:" + a + "the value of B is:" + B); System. o Ut. println (); System. out. println ("this is the transfer address reference:"); Difference diff = new Difference (); Difference diff1 = diff; // Let diff have the same reference System. out. println ("data before test:"); System. out. println ("diff. x = "+ diff. x + "diff1.x =" + diff1.x); diff. x = 100; System. out. println ("tested data:"); System. out. println ("diff. x = "+ diff. x + "diff1.x =" + diff1.x) ;}// the execution result is as follows/* This is a reference to the passed value: data before the test: the value of a is: 10 B: 10 data after the test: the value of a is 20 The value of B is: 10. This is the transfer address reference: the data before the test: diff. x = 10 diff1.x = 10 data after the test: diff. x = 100 diff1.x = 100 */Good. It is similar to some program examples in C/C ++. In fact, there are two ways to pass parameters in C ++: call by alues and call by reference are supported. In Java, only call by value is called. Not only will you be curious to ask, isn't diff a "Reference" passed in the above example? For www.2cto.com, this "Reference" is a reference in the Java World. It is different from the reference in C ++. The previous articles, such as arrays and strings, have the word "Reference". If you have any questions, please contact us and refer to the previous article. The example of the above program is explained as follows: passing value references are mainly for basic data types. The so-called passing value reference means that the actual value of the variable is transmitted during the variable transfer process, that is, the actual value is copied. Because one variable value does not affect the change of another variable value, the operation result does not affect the original value. The transfer address reference is mainly for object operations. It transfers the copy of an object handle. That is to say, when multiple variables operate on an object, any variable for the handle operation will affect other variables. Therefore, I think: "There is only one Call by value in the java World ). In fact, the above two methods pass "values". For an object, only the previous value is the address value of an object, the only difference is that it is similar to the "transfer Reference call", but it is actually a value transfer call. In C ++, there is also a Call for Reference transfer or Call by Reference ). "Many beginners are confused about the two concepts. The same is true for the author. Let's look at the following program example: we also use FindWife as an example. Run the following code: package com. ivantian. coreFengZhuang; // Object's calling by value (s) // Class Definitions class Wives {String name; Wives (String name) {this. name = name ;}// Testing, Finding wives public class FindWives {// FengJie, it's hard for me to control public static void FengJie (Wives w) {w. name = "FengJie";} // FuRong: All my well-behaved public static void FuRong (Wives w) {w = new Wives ("FuRong");} // main method public sta Tic void main (String [] args) {// replace FengJie with the white lady Wives wife = new Wives ("White lady"); FindWives. fengJie (wife); // can I replace it successfully? System. out. println ("My wife will be:" + wife. name); // replace FuRong with the white lady Wives wife1 = new Wives ("White lady"); FindWives. fuRong (wife1); // System. out. println ("My wife will be:" + wife1.name) ;}} what about the execution result? Here, I will first buy a customs sub, and then I will analyze it step by step. There are currently three amazing women "FengJie, FuRong, white lady" for me to choose from. If we use the theory of Nash equilibrium, we can quickly make an "optimal solution", but we are not doing Economics Research. Solution 1. Let her first make a comparison with FengJie. FengJie uses her own "skill Fengjie ()" to force the white lady wife to agree to Fengjie w to refer to her name. Of course, FengJie w is happy. Figure 1: the same object FengJie () of the wife and w references. My name is FengJie, which cannot be changed. So the white lady couldn't help her. Ii. Figure 2: specify the name of the object referenced by w as: After FengJie runs FengJie () in the main program, the object w in FengJie () is called by JVM) for "garbage collection (GC)", the output must obtain the wife. name. In fact, the last thing we get is the "White lady" object, but she uses the name of "FengJie. However, the JVM has no chance to hold her down. FengJie is a legend of this person, but no such person exists. FuRong also intends to use the same idea to deal with the white lady. Operation 3 is shown in Figure 3: wife1 and parameter w refer to the same object. Because FuRong witnessed the tragic ending of FengJie, she left her hand, A System Object w is created in FuRong () and assigned to w reference. 4. Figure 4: after the new object of w reference is completed in the main program FuRong, the newly created object w in FuRong () is sent to "garbage collection (GC)" by Fa Hai (JVM), and the output must obtain and display wife1.name. In fact, what we get is the "White lady" object, which is also her own name "White lady ". Unfortunately, FuRong is no longer a legend of FuRong. As shown in, the program execution result is:/* The program execution result is My wife will be: FengJie My wife will be: white lady */The above is My pair: java "value transfer call", please correct me!

Related Article

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.