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.