Value passing and reference passing in the Java language

Source: Internet
Author: User

Java The object is never passed in the application, but only the object is applied, so it is passed by object reference.

(1) When an int is passed as a parameter, the argument is worth modifying without affecting the arguments.

(2) When stringbuffer as a parameter, the modification of the content of the formal parameter object affects the actual parameter.

(3) A value pass simply passes a copy of the value, and the value of the argument is not affected by the change to the parameter.

(4) A reference is passed, because the address of the parameter is also passed as a value, so the address of the argument cannot be changed, only the value of the object to which the actual parameter address points is changed.

(5) The string object is passed, because the value of the string object cannot be modified, so when the string object is a parameter, the modification of the formal parameter does not affect the address of the argument, nor does it change the value of the object to which the argument address points. As an object reference, if the object contains a string-type property, the property's set method can be used to set the property, but the address value is changed.

(6) When a method is called, the modification of the property value of the object referred to by the parameter reference is visible to the argument. However, modifications to the reference value itself are not visible to the arguments.

String string1 = "123";

string1 = string1 + "345";

The definition of a string s is actually a pointer to a string object, string1 = String1 + "345"; A new string object is created to hold the new class capacity, and the original is still in memory, but s no longer points to it.

(String1 and "345" are string objects, strings are concatenated, which inevitably opens up space to store new string objects)

Value passing and reference passing in the Java language

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.