Java parameter passing (value passing or reference passing)

Source: Internet
Author: User

First, with three sentences to summarize, and then do the explanation: 1. The object is a reference, and the object changes directly to the object that the application points to. 2. The original type is the pass value, the transfer is worth the copy, the changes to the copy will not affect the original value. 3.String and other immutable types because they do not provide their own modified functions, each operation is a new object, so special treatment. Can be considered as a pass value. First, do not dwell on the literal meaning of pass by Value and pass by Reference, otherwise it is easy to fall into the so-called "all references in essence is the value of the transfer" This does not solve the problem of meaningless controversy. What's more, if you want to know whether Java is a value or a reference, at least you have to know the value of the transfer and the exact meaning of the reference it? But if you already know the exact meaning of these two names, then you can judge whether Java is a pass or a reference. It's like using a college term to explain a high school topic that doesn't make any sense to beginners. One: Figure out the difference between the basic type and the reference type
int num = ten= "Hello";
, num is the base type, and the value is stored directly in the variable. While STR is a reference type, the variable holds only the address of the actual object. This variable is generally referred to as a "reference", a reference to the actual object, and the contents stored in the actual object. Two: Figure out the function of the assignment operator (=)
num == "Java";
For the base type num, the assignment operator changes the value of the variable directly, and the original value is overwritten. For reference type STR, the assignment operator changes the address saved in the reference, and the original address is overwritten. But the original object will not be changed (important). As shown, the "Hello" string object has not been changed. (The object that is not pointed to by any reference is garbage and is reclaimed by the garbage collector) Three: What happens when the method is called? Parameter passing is basically an assignment operation.
///First example: Basic typevoidFoointvalue) {Value= 100;} Foo (num); //num has not been changed///Second example: No reference type is provided to change its own methodvoidfoo (String text) {text= "Windows";} Foo (str); //STR has not been altered .///Third example: Provides a reference type that changes its own method StringBuilder SB=NewStringBuilder ("iphone");voidfoo (StringBuilder builder) {Builder.append ("4");} Foo (SB); //SB has been changed into a "iphone4". ///Fourth Example: Provides a reference type that alters its own method, but uses the assignment operator instead of using it. StringBuilder SB=NewStringBuilder ("iphone");voidfoo (StringBuilder builder) {Builder=NewStringBuilder ("ipad");} Foo (SB); //SB has not been changed, or "iphone". 
Focus on understanding why, the third example and the fourth example results in a different result? The following is a schematic of the third example: Builder.append ("4") followed by a diagram of the fourth example: Builder = new StringBuilder ("ipad"); After Intopass Link: http://www.zhihu.com/question/31203609/answer/50992895 Source: The copyright belongs to the author of all, reproduced please contact the author to obtain authorization.

Java parameter passing (value passing or reference passing)

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.