Java: Pass value or pass reference?

Source: Internet
Author: User

This is a classic Java problem, most people from the C,c++ language introduction, C language has three kinds of delivery: value delivery, address delivery and reference delivery. Detailed pointers to C language, reference to my personal understanding, see links.

All Java operations are value-passing operations! It's all a value-transfer operation! It's all a value-transfer operation! The important thing to say three times.

Questions? Then why do people say that the basic data type of Java is a value, all the custom data (the object of the Class) is a reference??

Very simple, because it is so well understood that it means: "Students, if we pass a value (variable) of a basic data type to a function's formal parameter, then no matter what we do with this variable, the function will not change the value of the variable after it runs out!" (It's all right here) but if we pass an instance of a class (object) as a parameter to a function, then the change in the object in the function will actually change the value of the object! (not exactly right here) ".

For an understanding of Java objects and references, see: Talking about objects and references in Java. Let me briefly say that person person = new Person ("Zhang San"), in which the new person ("Zhang San") is an instance (or object) of the class person, which is a reference to the instance (object).

Java's parameter passing is a value pass!!!

Post a blog post: Understand reference passing and value passing in Java, and if you think this person makes a lot of sense, then you are Muggle-(wow!). Teacher Ma possessed! )。 I will not explain, to see the 1 floor review of this blog post!

We first define what is the value passing and what is the reference pass.

Value passing: When a method is called, the actual parameter passes a copy of its value to the corresponding formal parameter. Features: at this time there are two equal basic types in memory, that is, the actual parameters and formal parameters , the operation in the subsequent methods is to modify the value of the formal parameters, does not affect the actual parameter values .

Reference passing: When a method is called, the reference to the actual parameter (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and the function receives the memory address of the original value; Features: In the execution of the method, the formal parameter and the argument content are the same, point to the same piece of memory address will affect the actual object .

Value passing is simply too simple, it should be easy to understand that since the actual parameters are passed a copy, then change the copy, the original variable has no relation.

But for reference passing, I'm not saying the usual examples of support references. Look at a slightly special example: if we interpret the following example as a reference pass, the output should be: LI Si! Because Zhangsan this (new person ("ZHANG San") reference points to the new object! But the actual result is that Zhangsan the name of the object that the reference refers to does not change!

1  Public classValueorref {2      Public Static voidMain (string[] args) {3Person Zhangsan =NewPerson ("ZHANG San");4 Changeperson (Zhangsan);5 zhangsan.printname ();6     }7      Public Static voidChangeperson (person person) {8person =NewPerson ("LI Si");9     }Ten } One  A classPerson { -String name = "Default"; -      PublicPerson (String name) { the          This. Name =name; -     } -      Public voidchangename (String name) { -          This. Name =name; +     } -     voidPrintname () { +System.out.println ( This. Name); A     } at}

So here, we interpret this as "value passing", but the value here is a "reference" VALUE! That is, we take the actual parameter (a reference) copy a copy to the formal parameters, form parameters to operate. when the formal parameter changes the object itself, the effect here is the same as the reference pass. But once the form parameter is assigned a type of operation, the assignment does not reflect the effect of the assignment to the end of the function, as in the case of the C language.

Summary: Don't tangle with the concept of value passing and reference passing, we understand that the process of function execution is good! For a better understanding of the changes in the entire operating process, refer to: Java: Passing by value or by reference with detailed explanations (which have a picture presentation, very clearly).

Java: Pass value or pass reference?

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.