The difference between Java basic data type passing and reference passing is detailed

Source: Internet
Author: User
Tags call by reference

Java value Delivery and reference delivery are generally involved in interviews, and today we're going to talk about this, first of all we have to realize that this problem is generally relative to the function, that is, the method parameters in Java, So let's review the two professional terms in the programming language about parameters passed to methods (or functions):

    • Calling by value

    • Invoke by reference (call by reference)

The so-called call-to-value representation method receives the value provided by the call, whereas a call by reference means that the method receives the address of the variable provided by the caller (in the case of the C language, it is a pointer, and Java does not have a pointer concept, of course). Here we need to note that a method can modify the value of the variable to pass the reference, but not to modify the value of the variable passed the value of the call, which is very important, this is called by the value of the call and the reference to the fundamental difference, of course, if you do not understand, it is OK, the following will be illustrated thoroughly.

As we said before, there is no reference call in Java, which is true because the Java programming language does take a value call, called by values. This means that the method obtains a copy of all parameter values, and the method cannot modify the contents of any parameter variables passed to it. Let's look at an example:

[Java]View PlainCopy print?
  1. Package com.zejian.test;
  2. /**
  3. * Call by value in Java
  4. * @author Zejian
  5. */
  6. Public class Callbyvalue {
  7. private static int x=10;
  8. public static void Updatevalue (int value) {
  9. Value = 3 * value;
  10. }
  11. public static void Main (string[] args) {
  12. System.out.println ("The value of the pre-call x:" +x);
  13. Updatevalue (x);
  14. System.out.println ("value of x after call:" +x);
  15. }
  16. }

Run the program with the following results:

Value of the previous X call: 10

Value of x after call: 10

You can see that the value of x does not change, so let's take a look at the specific execution process:

Analysis:

1) value is initialized to a copy of the x value (that is, 10)

2) value is multiplied by 3 and equals 30, but note that at this point the value of x is still 10!

3) After this method is finished, the parameter variable value is no longer used and is recycled.

Conclusion: When passing method parameter types are basic data types (numbers and Boolean values), one method is not possible to modify the parameters of a base data type.

Of course, in Java, in addition to the basic data types and reference data types, which is the object reference, then what is the case for this data type? Let's just look at one example:

Declare a User object type:

[Java]View PlainCopyprint?
  1. Package com.zejian.test;
  2. Public class User {
  3. private String name;
  4. private int age;
  5. Public User (String name, int age) {
  6. This.name=name;
  7. This.age=age;
  8. }
  9. Public String GetName () {
  10. return name;
  11. }
  12. public void SetName (String name) {
  13. this.name = name;
  14. }
  15. public int getage () {
  16. return age;
  17. }
  18. public void Setage (int.) {
  19. this.age = age;
  20. }
  21. }

The execution classes are as follows:

[Java]View PlainCopyprint?
  1. Package com.zejian.test;
  2. /**
  3. * Call by value in Java
  4. * @author Zejian
  5. */
  6. Public class Callbyvalue {
  7. private static User user=null;
  8. public static void UpdateUser (User student) {
  9. Student.setname ("Lishen");
  10. Student.setage (18);
  11. }
  12. public static void Main (string[] args) {
  13. user = New User ("Zhangsan",26);
  14. System.out.println ("value of user before call:" +user.tostring ());
  15. UpdateUser (user);
  16. System.out.println ("value of user after invocation:" +user.tostring ());
  17. }
  18. }

The results of the operation are as follows:

Value of user before call: User [Name=zhangsan, age=26]

Value of user after invocation: User [Name=lishen, age=18]

Obviously, the value of the user is changed, that is, if the method parameter type is a reference type, the value corresponding to the reference type will be modified, let's analyze This process:

Process Analysis:

1) The student variable is initialized to a copy of the user value, and here is a reference to an object.

2) The Set method that invokes the student variable acts on the referenced object, and the internal values of the user object referenced by both user and student are modified.

3) After the method is finished, the student variable is no longer used, is freed, and the user remains unchanged, pointing to the user object.

Conclusion: When the pass-through method parameter type is a reference data type, a method modifies the value of the object pointed to by a parameter of the reference data type.

Although the transfer of the two data types is analyzed here, it is also understood that the transfer of the basic data type and the pass-through of the reference data type will not modify the value of the original data, while the latter will modify the value of the object pointed to by the reference. With the above example, we might think that Java has both call-by-value and invoke-by-reference, but it is a pity that such an understanding is misleading, although the reference-passing surface above refers to the invoke-by-reference phenomenon, but in Java it is true that only calls by value are not invoked by reference. It is estimated that quite a few people have been forced to do so, so let's take a counter-example to illustrate (recall the fundamental difference between the call by value and the call by reference, which we described at the beginning).

[Java]View PlainCopyprint?
  1. Package com.zejian.test;
  2. /**
  3. * Call by value in Java
  4. * @author Zejian
  5. */
  6. Public class Callbyvalue {
  7. private static User user=null;
  8. private static User stu=null;
  9. /** 
  10. * Exchange of two objects
  11. * @param x
  12. * @param y
  13. */
  14. public static void swap (User x,user y) {
  15. User temp =x;
  16. X=y;
  17. Y=temp;
  18. }
  19. public static void Main (string[] args) {
  20. user = New User ("User",26);
  21. Stu = new User ("Stu",18);
  22. System.out.println ("value of user before call:" +user.tostring ());
  23. System.out.println ("Stu value before calling:" +stu.tostring ());
  24. Swap (USER,STU);
  25. System.out.println ("value of user after invocation:" +user.tostring ());
  26. System.out.println ("Stu Value after call:" +stu.tostring ());
  27. }
  28. }

We swapped two variables for the value of the user and the Stu by a swap function, as we said earlier, if it is called by reference, then a method can modify the value of the variable to pass the reference, that is, if Java is invoked by reference, then the Swap method will enable the exchange of data The actual running result is:

Value of user before call: User [Name=user, age=26]

Value before call Stu: User [Name=stu, age=18]

Value of user after invocation: User [Name=user, age=26]

Value of Stu after call: User [Name=stu, age=18]

We found that the value of user and Stu did not change, that is, the method did not change the object references stored in the variables user and Stu. The parameters x and y of the swap method are initialized to a copy of the two object references, and this method swaps the values of the two copies, and in the end, it's a waste of effort. X, Y will be discarded after the method ends, and the original variable user and Stu still refer to the object referenced before the method call.

This process also fully illustrates that the Java programming language is not referring to the object invocation, is actually the object reference is the value of the pass, of course, here we can simply understand that this is the difference between call by value and reference call, and must understand that even if the Java function in passing the reference data type, It's just a copy of the value of the reference, so the reference data can be modified because they point to an object at the same time, but this is still called by value rather than by reference.

Summarize:

      • A method cannot modify the parameters of a base data type (numeric and Boolean).

      • A method can modify the state of an object pointed to by a reference, but this is still called by value rather than by reference.

      • Both of the above passes have a value copy process.

The difference between Java basic data type passing and reference passing is detailed

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.