Analysis of the difference between value passing and reference passing in Java (with code)

Source: Internet
Author: User
Tags data structures
This article brings you to the content of Java in the value of delivery and reference delivery (address delivery) between the analysis of the difference (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

A value pass (pass by value) refers to a copy of the actual argument to the function when the function is called , so that if the parameter is modified in the function, will not affect the actual parameters. Transitive objects are often basic data structures such as Integer floating-point character types.

public class Passbyvaluereference {//value passed public static void main (string[] args) {int x = 9;pass (x); SYSTEM.OUT.PRINTLN (x);} private static void pass (int y) {System.out.println (y); y=0;}}

Run Result: (the value change of integer y does not affect the value of integral type X)

Reference passing (pass by reference) means that the address of theactual parameter is passed directly to the function when the function is called, and the modification of the parameter in the function will affect the actual parameter. (similar to a community) transitive objects tend to be arrays of address data structures.

public class Passbyvaluereference {        //reference pass public static void main (string[] args) {int [] x = {9};                System.out.println (X[0]);p (x); System.out.println (X[0]);} public static void Pass (int [] y) {y[0] = 0;}}

Run Result: (the value change of the array y affects the value of the array x at the same time)

(Value passing and reference passing the knowledge of stacks and heaps in the computer data structure)

In addition, if you have not touched the knowledge of functions and methods in Java, you can simply use the assignment to understand: (running results similar to the above)

public class Passbyvaluereference {//value pass (Assignment non-function) public static void main (string[] args) {    int x = 9;            SYSTEM.OUT.PRINTLN (x);    y = x;            y = ten;    SYSTEM.OUT.PRINTLN (x);}}

Here x and Y are the basic data types, assigning the value of x to Y, which is equivalent to copying a copy, rather than giving the entire x (that is, the X address) to Y.

public class Passbyvaluereference {        //reference Pass (Assignment non-function) public        static void Main (string[] args) {int [] x = {1}; System.out.println (x[0]); int [] y = x;y[0] = 0; System.out.println (X[0]);}}

Here x and Y is an array, this time the code is not simply to make x[0]=y[0], but directly to the x=y, this assignment statement to the X array address to Y, equivalent to "on the same boat", change together, y[0] changed at the same time x[0] also changed.

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.