Java self-study notes Series 7: encapsulation (3) Java value transfer call

Source: Internet
Author: User
Tags call by reference

Let's take a look at the following code:

 
 
  1. Package com. ivantian. CoreFengZhuang;
  2.  
  3. // Call by values
  4. // Call by address
  5. Public class Difference {
  6.  
  7. Int x = 10; // member variable
  8. Public static void main (String [] args ){
  9. Int a = 10;
  10. Int B =;
  11. System. out. println ("this is a value reference :");
  12. System. out. println ("data before test :");
  13. System. out. println ("the value of a is:" + a + "the value of B is:" + B );
  14. A = 20;
  15. System. out. println ("tested data :");
  16. System. out. println ("the value of a is:" + a + "the value of B is:" + B );
  17. System. out. println ();
  18. System. out. println ("this is the transfer address reference :");
  19. Difference diff = new Difference ();
  20. Difference diff1 = diff; // give diff the same reference
  21. System. out. println ("data before test :");
  22. System. out. println ("diff. x =" + diff. x + "diff1.x =" + diff1.x );
  23. Diff. x = 100;
  24. System. out. println ("tested data :");
  25. System. out. println ("diff. x =" + diff. x + "diff1.x =" + diff1.x );
  26. }
  27. }
  28. // The execution result is as follows:
  29. /*
  30. This is a value reference:
  31. Data before testing:
  32. The value of a is 10. The value of B is 10.
  33. Tested data:
  34. The value of a is 20. The value of B is 10.
  35.  
  36. Here is the transfer address reference:
  37. Data before testing:
  38. Diff. x = 10 diff1.x = 10
  39. Tested data:
  40. Diff. x = 100 diff1.x = 100
  41.  
  42. */

Yes, it is similar to some program examples in C/C ++. In fact, there are two ways to pass parameters in C ++: call by alues by passing values) and call by reference. In Java, only call by value is passed. Not only will you be curious to ask, isn't diff a "Reference" passed in the above example?

Yes. This "Reference" is a reference in the Java World, which 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 preceding program example is explained as follows:

Value-passing 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 transferring Reference or calling Call by Reference ). "

Many beginners are confused with 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.

First run the Code:

 
 
  1. Package com. ivantian. CoreFengZhuang;
  2.  
  3. // Object's calling by value (s)
  4.  
  5. // Class Definitions
  6. Class Wives {
  7. String name;
  8. Wives (String name ){
  9. This. name = name;
  10. }
  11. }
  12.  
  13. // Testing, Finding wives
  14.  
  15. Public class FindWives {
  16.  
  17. // FengJie is hard to handle
  18. Public static void FengJie (Wives w ){
  19. W. name = "FengJie ";
  20. }
  21. // FuRong: All my well-behaved and sensible
  22. Public static void FuRong (Wives w ){
  23. W = new Wives ("FuRong ");
  24. }
  25. // Main method
  26. Public static void main (String [] args ){
  27. // Replace FengJie with the white lady
  28. Wives wife = new Wives ("White lady ");
  29. FindWives. FengJie (wife); // is the change successful?
  30. System. out. println ("My wife will be:" + wife. name );
  31. // Replace FuRong with the white lady
  32. Wives wife1 = new Wives ("White lady ");
  33. FindWives. FuRong (wife1 );//
  34. System. out. println ("My wife will be:" + wife1.name );
  35. }
  36. }

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:

As a matter of fact, it is good to look for the "White lady". Bai fumei is still able to go to the ground. 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. 1:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/12041B394-0.png "/>

Figure 1: wife and w refer to the same object

FengJie () says, my name is FengJie, which cannot be changed. So the white lady couldn't help her. II:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1204163N7-1.png "/>

Figure 2: specify the name of the object referenced by w as FengJie.

After the main program executes FengJie (), the object w in FengJie () is sent to "garbage collection GC" by fhai JVM. The output must get the wife. name and display it. 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 intends to use the same idea to deal with the white lady. Operation 3 is shown below:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1204162E8-2.png "/>

Figure 3: wife1 and parameter w refer to the same object

FuRong witnessed the tragic ending of FengJie. She left her hand and set up a system object w in FuRong () and assigned it to w for reference,

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1204161b0-3.png "/>

Figure 4: new object referenced by w

After the main program executes FuRong (), the newly created object w in FuRong () is sent to "garbage collection GC" by Fa Hai JVM. 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:

 
 
  1. /* Program execution result
  2. My wife will be: FengJie
  3. My wife will be: white lady
  4. */

The above is my opinion on: Java "value transfer call". please correct me!

This article is from the "IvanTian" blog, please be sure to keep this source http://ivantian2008.blog.51cto.com/622133/1041043

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.