Java FAQ series (v) -- has the value passed or the reference passed?

Source: Internet
Author: User
2008-07-13java FAQ series (5) -- passed the value or passed the reference?

Keywords: Java interview question value transfer reference TransferAuthor: zookeeper (zangweiren)

Web: http://zangweiren.javaeye.com

>>>Reprinted please indicate the source!<

Are values transmitted in Java? Is there any reference transfer?

Before answering these two questions, let's take a look at the Code:
Java code

  1. Public class paramtest {
  2. // The initial value is 0.
  3. Protected int num = 0;
  4. // Assign a value to the method parameter
  5. Public void change (int I ){
  6. I = 5;
  7. }
  8. // Assign a value to the method parameter
  9. Public void change (paramtest t ){
  10. Paramtest TMP = new paramtest ();
  11. TMP. num = 9;
  12. T = TMP;
  13. }
  14. // Change the value of the method parameter
  15. Public void add (int I ){
  16. I + = 10;
  17. }
  18. // Change the value of the method parameter attribute
  19. Public void add (paramtest pt ){
  20. PT. Num + = 20;
  21. }
  22. Public static void main (string [] ARGs ){
  23. Paramtest T = new paramtest ();
  24. System. Out. println ("parameter -- Basic Type ");
  25. System. Out. println ("Original Value:" + T. Num );
  26. // Assign a value for the basic type parameter
  27. T. Change (T. Num );
  28. System. Out. println ("after assignment:" + T. Num );
  29. // Assign a value to the referenced Parameter
  30. T. Change (t );
  31. System. Out. println ("after calculation:" + T. Num );
  32. System. Out. println ();
  33. T = new paramtest ();
  34. System. Out. println ("parameter -- reference type ");
  35. System. Out. println ("Original Value:" + T. Num );
  36. // Change the value of the basic type parameter
  37. T. Add (T. Num );
  38. System. Out. println ("after being referenced:" + T. Num );
  39. // Change the property value of the object to which the reference type parameter points
  40. T. Add (t );
  41. System. Out. println ("after modifying the attribute:" + T. Num );
  42. }
  43. }
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 running result of this Code is as follows:

  1. Parameter -- Basic Type
  2. Original Value: 0
  3. After Value assignment: 0
  4. After calculation: 0
  5. Parameter -- reference type
  6. Original Value: 0
  7. After being referenced: 0
  8. After modifying attributes: 20

From the above intuitive results, we can easily draw the following conclusions:

  1. For the basic type, the method parameter is assigned a value again in the method body, and the value of the original variable is not changed.
  2. For the reference type, the method parameter is re-referenced in the method body, and does not change the reference of the original variable.
  3. In the method body, parameters are calculated without affecting the value of the original variable.
  4. In the method body, operations are performed on the attributes of the object to which the parameters point. The attribute values of the objects to which the original variables point are changed.

What we have summarized above is what we see on the surface. So why does this happen? This requires the concept of value transfer and reference transfer. This issue has always been quite controversial.

As we all know, there are two types of variation in Java:

  1. Basic type variables, including char, byte, short, Int, long, float, double, and Boolean.
  2. Reference type variables, including classes, interfaces, and arrays (basic type arrays and object arrays ).

When a variable of the basic type is passed as a parameter to a method, the Java Virtual Machine copies the value and then transmits the copied value to the method. So in the above example, let's look back at this method:
Java code

  1. // Assign a value to the method parameter
  2. Public void change (int I ){
  3. I = 5;
  4. }
// Assign public void change (int I) {I = 5;} to the method parameter again ;}

When this method is called, the attribute num of variable I and paramtest object T has the same value, but it is two different variables. Variable I is the scope created by the Java Virtual Machine in
The local variable in the change (int I) method ends after the method is executed. In Java virtual machines, they are stored in a similar way:

Obviously, when the basic type is passed as a parameter to the mode, it is a value transfer, and the reference concept is not involved in the whole process. This is also widely accepted. This is also true for Boolean variables. See the following example:
Java code

  1. Public class booleantest {
  2. // Boolean Value
  3. Boolean bool = true;
  4. // Assign a value to a Boolean Parameter
  5. Public void change (Boolean B ){
  6. B = false;
  7. }
  8. // Calculate Boolean Parameters
  9. Public void calculate (Boolean B ){
  10. B = B & false;
  11. // Output the calculation result for convenience of comparison
  12. System. Out. println ("value after B operation:" + B );
  13. }
  14. Public static void main (string [] ARGs ){
  15. Booleantest T = new booleantest ();
  16. System. Out. println ("parameter -- Boolean ");
  17. System. Out. println ("Original Value:" + T. bool );
  18. // Assign a value to a Boolean Parameter
  19. T. Change (T. bool );
  20. System. Out. println ("after assignment:" + T. bool );
  21. // Change the value of the Boolean Parameter
  22. T. Calculate (T. bool );
  23. System. Out. println ("after calculation:" + T. bool );
  24. }
  25. }
Public class booleantest {// Boolean bool = true; // the value of public void change (Boolean B) {B = false for the Boolean parameter ;} // calculate the public void calculate (Boolean B) {B = B & false; // output the calculation result to system for convenience of comparison. out. println ("value after B operation:" + B);} public static void main (string [] ARGs) {booleantest T = new booleantest (); system. out. println ("parameter -- Boolean"); system. out. println ("Original Value:" + T. bool); // re-assign t for the Boolean parameter. change (T. bool); system. out. println ("after assignment:" + T. bool); // change the value of the Boolean parameter T. calculate (T. bool); system. out. println ("after calculation:" + T. bool );}}

The output result is as follows:

  1. Parameter-Boolean
  2. Original Value: True
  3. True
  4. Value After B operation: false
  5. After calculation: True

How does the Java Virtual Machine Process referenced variables as parameters when they are passed to the method? Similarly, it will copy a reference held by this variable and pass it to Java virtual
The local variable created by the machine for the method, and the two variables point to the same object. In the example at the beginning of the article, the paramtest type variables T and local variables PT in the Java Virtual Machine are as follows:
Storage:

There is a saying that when an object or a reference type variable is passed as a parameter, it is also a value transfer. This value is the reference of the object. Therefore, only the value is passed in Java and no reference is passed. There is also
Reference can be regarded as the alias of an object. when an object is passed as a parameter to a method, the object is referenced and therefore passed as a reference. These two views have their own supporters, but the previous view is largely
People accept this, including the author of core Java and the creator of Java, James Gosling, and thinking in
Bruce Eckel, author of Java, stands on a neutral standpoint.

In my opinion, the value in the value transfer refers to a value of the basic type. Even if the boolean type is expressed as true or false, in the stack, it is still saved as a numerical value.
0 indicates false, and other values indicate true. Reference is a tool used to operate objects. It contains information about the address of objects stored in the heap. Even if it is passed to the method as a parameter
It is a copy, but it is still referenced. Therefore, it is more clear in concept that the difference and value transfer are implemented through reference transfer.

Finally, we come to the following 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 as a parameter to a method, 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.