Reference: https://blog.csdn.net/zhenge1990/article/details/78897166
Java method of delivery, there are two parameters, and the value is passed reference.
1. passed by value:
Parameter type int, long, etc. eight basic data types, the process parameters are passed by value of the copy process
The following code
public static void main (String [] args) { int A =. 5 ; Fun (A); System.out.println (A); // output result. 5 } Private static void Fun ( int A) { A +. 1 = ; }
2. passed by reference
Type process parameter reference type, parameters passed by way of reference copy
public class Test { public static void main(String[] args) { A a = new A(5); fun(a); System.out.println(a.a);// 输出结果为6 } private static void fun(A a) { a.a += 1; } static class A { public int a; public A(int a) { this.a = a; } } }
Look at the following situation:
public class the Test { public static void main (String [] args) { Integer A =. 5 ; Fun (A); System.out.println (A); // output result. 5 } Private static void Fun (Integer A) { A + =. 1 ; } }
Here obviously is passed by reference, why not change the value of the object of it?
Here the basic data types actually used autoboxing function package type.
Integer a = 5, actually compiled Integer a = Integer.valueOf (5), see Integer source, does not change the value of the original object, but which is a reference to point to another object.