NO. 002 Bullet: Value passing and reference passing in Java

Source: Internet
Author: User

In the Java code development process, in order to maximize the reuse of the method, to clarify the role of the method, while preventing a method internal too bloated problem, often create many methods, then inevitably involves the problem of parameter passing. In general, we divide the parameters in Java into two types: value passing and reference passing.

    • Value passing: When a parameter enters a method, it copies a copy of the entry depth, and a copy of the incoming parameter is manipulated inside the method, and after the method has been executed, there is no change in the external entry parameters.
    • Reference passing: The parameter itself is manipulated inside the method, and changes made to the incoming parameter are persisted to the outside of the method.

So in Java, what are the values that are passed and what are the reference passes?

1. Types of incoming arguments

A false view is widely circulated: If the entry is a basic type, it is a value pass, or if the entry is not a base type, it is a reference pass. Or, go into a little bit more, and relate to the location where the entry is stored. (the base type is stored in the stack, and the object is stored in the heap)

The above statement is not entirely correct. The first half of the sentence is basic, but for the second sentence, I can easily find the following counter-example:

The object that is generated by the string literal, is a non-basic type stored in the heap. According to the above view, this parameter should be passed by reference, then the modification of the string should be saved to the change (string) method, but the final output is not. Change the string to StringBuffer, do a similar operation, and the result is consistent with the string.

Conclusion: The basic type of parameter transfer, must be the value of the transfer, but the non-basic type of parameters, which is not necessarily passed by reference delivery, need to be further analyzed.

2. Return type of the method

In addition, there is a mistaken view of how the parameter is passed, and whether the method has a return value, and if a method has a return value, then the parameter must be passed by value. Consider the following example:

1      Public Static voidMain (string[] args) {2person P1 =NewPerson ();3P1.setage (20);4P1.setgender (0);5P1.setname ("haha");6person P2 =NewPerson ();7 Beanutil.copysamefieldsobject (P1, p2);8 Change1 (p1);9 System.out.println (p1);Ten change2 (p2); One System.out.println (p2); A     } -  -     Private Static voidchange1 (person p) { theP.setage (30); -P.setgender (1); -P.setname ("hehe"); -     } +  -     Private StaticPerson change2 (person p) { +P.setage (30); AP.setgender (1); atP.setname ("hehe"); -         return NULL; -}
Test1

If the argument is passed in a way that is related to the return value of the method, then the two output results must be different (the person class has overridden the ToString () method), because the first method is a reference pass, and the second method is the value pass, But actually the output of two times is the same.

Conclusion: The parameter is passed in a way that has no relation to whether the method has a return value or the type of the return value.

3. Factors that really determine the way in which the transfer is made

For non-primitive types of arguments, their parameters are passed in a variable manner. Take a look at the following example:

1      Public Static voidMain (string[] args) {2person P1 =NewPerson ();3P1.setage (20);4P1.setgender (0);5P1.setname ("haha");6person P2 =NewPerson ();7 Beanutil.copysamefieldsobject (P1, p2);8Person P3 =NewPerson ();9 Beanutil.copysamefieldsobject (P1, p3);Ten Change1 (p1); One System.out.println (p1); A change2 (p2); - System.out.println (p2); - Change3 (p3); the System.out.println (p3); -     } -  -     Private Static voidchange1 (person p) { +P.setage (30); -P.setgender (1); +P.setname ("hehe"); A     } at  -     Private Static voidchange2 (person p) { -p =NewPerson ("hehe", 1, 30); -     } -      -     Private Static voidchange3 (person p) { inP.setname ("hehe"); -p =NewPerson ("hehe", 1, 30); to}
Test2

In the above procedure, the function of the Beanutil.copysamefieldsobject () method is to copy the contents of the first parameter in depth, give the second parameter, and output the result as shown:

The following conclusions can be drawn from the above studies:

    • Arguments for non-primitive types can be passed as a reference pass, but according to code executed inside the method, there are variables that can be converted to value passing.
    • It can be thought that the method's entry is a reference to an object, which is recorded as P, which is stored in the stack, which points to a memory in the heap and is recorded as Q. When the entire method only modifies the contents of Q, and p always points to Q, the entire parameter can be passed as a reference pass.
    • When inside the method, if the code attempts to point p to another piece of memory T, then the JVM creates another reference R, causes R to point to T, and P still points to Q.
    • This attempt to change the behavior pointed to by the pointer is primarily to create a new object. The specific method of creating the object, see the contents of the previous section. Several special forms are also included, such as the use of the operator "=" to create an object of type String. Additional attention is paid to some methods, where the internal implementation uses the new creation object, such as the String.Concat (String) method.
    • The 8 wrapper types of the base type, which can be treated as basic types, are passed as a reference, but can be considered equivalent to value passing. Because the 8 wrapper types are the same as the String type, their internal data is immutable, which means that any change is essentially a new object in memory.

NO. 002 Bullet: Value passing and reference passing in Java

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.