Java value passing and reference passing

Source: Internet
Author: User

Value passing:

When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the function receives a copy of the original value, at which time there are two equal basic types in memory , that is, the actual parameter and the formal parameter, and the operation in the subsequent method is the modification of the value of the parameter. Does not affect the value of the actual parameter .

Reference delivery:

Also known as a pass-through address. When a method is called, the actual argument's reference (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;
In the execution of the method, the parameters and the arguments are the same, pointing to the same memory address, and the operation of the reference in the method execution will affect the actual object.

(1) The basic data type transmission value, the modification of the formal parameters will not affect the actual parameters;
(2) Reference type is referenced, formal parameters and arguments point to the same memory address (the same object), so the modification of parameters will affect the actual object;
(3) String, Integer, double, etc. (wrapper class) immutable type special processing, can be understood as the value of the pass, the last operation will not modify the argument object, and the basic type is similar.

 Public classTeststringtransport { Public voidChange (String str) {str= str + "World"; }         Public voidChange (StringBuffer sb) {sb.append ("World"); }         Public Static voidMain (string[] args) {teststringtransport test=NewTeststringtransport (); String Str= "Hello";        Test.change (str);                System.out.println (str); String str1=NewString ("Hello");        Test.change (STR1);                System.out.println (STR1); StringBuffer SB=NewStringBuffer ("Hello");        Test.change (SB);    System.out.println (SB); }}

Results:

Hello
Hello
HelloWorld

Java value passing and 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.