Java value passing and reference passing

Source: Internet
Author: User

Excellent blog forwarded from: http://blog.csdn.net/zgz0627/article/details/50180971

Only value is passed in Java, and no reference pass exists. The value is passed either as a value or as a pass-through address. Note that a pass-through address is not a reference pass.

First, the value of transmission

1. code example:

  1. Public class Valuepropagation
  2. {
  3. static void ChangeValue (int value)
  4. {
  5. Value + = 10;
  6. }
  7. public static void Main (string[] args)
  8. {
  9. int num = 10;
  10. //The value of num before the ChangeValue method was executed
  11. SYSTEM.OUT.PRINTLN (num);
  12. ChangeValue (num);
  13. //The value of NUM after executing the ChangeValue method
  14. SYSTEM.OUT.PRINTLN (num);
  15. }
  16. }

2. Results: The results of the two outputs were the same, and all 10 did not change.

3. Illustration:

4. Analysis: When NUM is passed as a parameter to the ChangeValue (int value) method, the value in the memory cell to which num points in memory space is passed to the parameter value variable in changevalue (int value), And the value of this variable is also allocated in the memory space of a memory unit, changevalue (int value) All operations are to the value of the memory cell point to the values of the operation, and Num refers to the memory unit no longer have any relationship, So the NUM value of two outputs is the same.

Ii. Address of the communication

When an instance object is passed as a parameter, a copy of the object reference is passed, the address value is transmitted, and now the object has two references pointing to her, the object's contents can be changed during the call, but the object's reference is always unchanged, the object reference is passed as a parameter, the reference is not changed, and obviously this is a value pass.

1. code example:

  1. Class Person
  2. {
  3. public static String name = "Tom";
  4. }
  5. Public class Transmitadress
  6. {
  7. static void ChangeName (person p)
  8. {
  9. P.name = "Jerry";
  10. }
  11. public static void Main (string[] args)
  12. {
  13. Person person = new Person ();
  14. //ChangeName (person p) before
  15. System.out.println (Person.name);
  16. ChangeName (person);
  17. //After executing changename (person p)
  18. System.out.println (Person.name);
  19. }
  20. }

2. Result: The first output was Tom, the second output of Jerry. Faced with such a result, many people can easily think of this as a reference to pass through the following illustrations and analysis you will understand that this is also the value of the pass.

3. Illustration:

4. Analysis: The main function new a person object, actually allocated two memory space: one in the heap memory, used to hold the object entity to create the person class, one in the stack memory, to hold the reference variable to the object person. 0x345~0x348 This heap of memory is the entity of the object, 0x345 is the first address of the entity object, and the stack memory that the person points to is 0x345, which is the first address of the entity object, that is, the person points to the entity object. When the person is passed as a parameter to the ChangeName (person p) method, the value stored in the memory unit pointed to by the person in the stack memory is copied to the parameter p, that is, the address of the entity object is passed to p, and the parameter P has been allocated a memory unit in the stack memory. All operations in ChangeName (person P) operate on the memory unit referred to by P, which is not related to the memory unit that the person points to. So, this is a value pass, not a reference pass.

Java value passing and reference passing

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.