How do you understand that parameter passing in Java can only pass values?

Source: Internet
Author: User

Before learning C #, is completely in the work of learning, some of the bottom of the more in-depth truth is not very clear. Now that you have learned Java, it is confusing to pass a value to a Java parameter, not a reference (pointer), not often in C # to pass a reference to a function? Even C # has a fairly simple ref, out parameter, plainly quoted.

After some exploration, the conclusion shows that in Java I don't care whether you are a value or a reference, you just have to remember that the original data type (value type) and string are passed as arguments, and the value of the reference type is not changed when it is passed as a parameter. Will be fed back to the value pointed to by the reference.

This conclusion is not the same as C #, except C # can be conveniently used ref, out ...

The following can be interpreted only by passing values to the parameters in Java, and it turns out that I did not understand the value of C # in the past or the reference to it in depth:

Firstof all: parameter Passing in Java is a value pass and no reference is passed.
The concept of value passing: value passing will re-open a space for the object being passed (the object here is not an object in Java, but an object in the general sense), so the manipulation of the object will not affect the original object.
Phenomenon: There is an argument that the basic data type in Java is a value pass, and the object is a reference pass. It is actually wrong to pass the true reference, which is similar to the C language of pointer passing, and the ref parameter in C #.
Understand: Why does an object make a value pass as a parameter?
Analysis: Objects are passed the object's reference, can be understood as the object's address. Because it is a value pass, the passed object itself will not have any change, so the object's reference will not be changed.

1. First, give an example of a native data type:

Test function:

     Public Static void Valuetrans (int  a) {        = 2;   A new value is created that creates a value type, so the value is not modified.     }

To test the code for this function:

        int a = 1;        System.out.println ("A ' s value (before) =" + a);        Valuetrans (a);        System.out.println ("A ' s value (after) =" + a);

Print out the results:

A ' s value (before) = 1a ' s value (after) = 1

The idea of installing Java value Transfer is considered, the result is obvious, pass by value, I just pass this value to you to use.

2, then how to understand the integer?

Test function:

     Public Static void ValueTrans2 (Integer a) {        new integer (2);    }


Where A is also an object, so the incoming, is not a reference it? If this function is called, will the int value of a be modified?

To test the code for this function:

        New Integer (1);        System.out.println ("A2 ' s value (before) =" + A2);        VALUETRANS2 (A2);        System.out.println ("A2 ' s value (after) =" + A2);

Print out the results:

A2 ' s value (before) = 1a2 ' s value (after) = 1

The results show that the values are still not modified.

How to explain this phenomenon:

The key to understanding this is to differentiate between objects and references. The A2 declared here is a reference, not an object (just Java designs it to look like an object). This reference points to an object that is the object that is generated later with the New keyword. Therefore, it can be said that A2 points to an integer object.
When calling the ValueTrans2 method, the program passes A2 as a parameter to the ValueTrans2 method. This is still a value pass, and a new reference (called Y) is generated during the VALUETRANS2 call. At this point, A2 and Y point to the same object.

But at this point, we let y point to another object, Y=new Integer (2); At this point, A2 and Y point to different objects. Y modifies the properties of the object it points to, and it obviously does not affect the object that A2 points to.

(Here's a description of the passing of the reference type, as I pass a reference type X into the function, and here is the value passing, and in the process of the function being called, a new reference y is generated,and X and Y point to the same object .) At this point, modify the object that Y points to, or modify the object that X points to! Then the passing of the reference type will cause the value that X points to be modified)

The illustrations are as follows:

3. How to understand string?

If you understand the way the integers are passed in 2, then I think string is not difficult to understand. We create a new string,

String string = "Old string";


is actually

New String ("Old String");

Java mechanism that causes a new object to be created when you use a string such as "Old String".

In a responsible manner, the corresponding test functions are still affixed:

     Public Static void Stringtrans (String string) {        = "New String";    }

Code for the test function:

        String string = "Old string";        System.out.println ("string ' s value (before) =" + string);        Stringtrans (string);        System.out.println ("string ' s value (after) =" + string);

Print out the results:

String ' s value (before) = Old stringstring ' s value (after) = Old String

The reason is that, in fact, the function Stringtrans is copying a reference to the original object, but the function also makes the reference point to the new string object. This will not have any effect on the original reference and the original object!

4, how to understand the reference type is a value?

To test your custom class:

 Public class people {    publicint  : Age    ;  Public String name;          Public People (int  , String name) {        this. Age = Age ;          this. Name = name;}    }

Function of the test:

     Public Static void Refertrans (People people) {        = 2;         = "Lisi";    }

Code for the test function:

        New People (1, "Zhangsan");        System.out.println ("People ' s age (before) =" + people.age);        System.out.println ("People ' s name (before) =" + people.name);        Refertrans (people);        System.out.println ("People ' s age (after) =" + people.age);        System.out.println ("People ' s name (after) =" + People.name);

Printed results:

People ' s age (before) = 1people's name (before) = Zhangsanpeople ' s age (after) = 2people ' s name (after) = Lisi

Set the new object is people, its reference is people, in the call Refertrans method is, is still the value of the pass, the method assigns a reference people2,people2 still point to people, so the operation People2 object modification, That is to modify the people (string words at this time has been new to come out), and use the same effect as people. The appearance of the phenomenon is that when a reference type is passed into a function, its modification in the function will appear on the original reference type.

The illustrations are as follows:

I have always thought that only to the analysis process, not to the conclusion is bullying behavior.

Conclusion :

1, in the actual use of the process, we do not have to tangle so much, the original data type and string, in the passing of the function, no matter what the function inside, his value will not be modified. The reference type is passed to the function, and the modification in the function is reflected on this reference type as long as the function does not have the argument point to a new object .

2, ordinary processing, you can put the string and the original data type in the same class of cases, because Java uses a lot of mechanisms, so that the string looks like the native data type. But string is ultimately a reference type, such as when comparing, other native data types can use = =, and string must use the Equals method to compare its internal values (this is a very good C #, directly use = =, do not let programmers too much knowledge of the underlying, I am doing C # In the process of programming, you never even feel the difference between a string and a value type.

3, Java really can only pass value, cannot pass the reference . The so-called reference is only present in C, C + + pointers and in C # ref.

How do you understand that parameter passing in Java can only pass values?

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.