Summary:
1. When an object (Object Reference) is passed as a parameter, a reference (equivalent to a pointer) is passed ). That is to say, the changes made to parameters in the function will affect the original objects.
2. When the basic type or basic type package set is passed as a parameter, the value is passed. That is to say, the changes made to parameters in the function do not affect the original variables.
3. When an array (array reference) is passed as a parameter, a reference (equivalent to a pointer) is passed ). That is to say, the changes made to parameters in the function will affect the original array.
4. when a string type (reference) is passed as a parameter, a New String object is generated when any modifications are made to the string type, and the value of the original string object is not modified. (However, the reference of a new object can be assigned to the original reference. The superficial phenomenon for people is that the original object has changed, but it has not actually changed, only the reference pointing to it points to the new object ).
Package stringtest; Class A {int A = 1; char B = 'a'; Public A () {} public a (INT _ A, char _ B) {This. A = _ A; this. B = _ B;} Public String tostring () {return "A =" + this. A + ", B =" + this. B ;}} public class referencetest {public static A Changea (A classa) {classa. A = 2; classa. B = 'B'; return classa;} public static string changestring (string Str) {system. out. println (Str. hashcode (); STR = Str. tolowercase (); system. out. println (Str. hashcode (); Return STR;} public static int changeint (int A) {A = a + 1; return a;} public static integer changeinteger (integer) {A = new INTEGER (9); return a;} public static int [] changeintarray (int A []) {A [0] = 10; return ;} public static void printarray (int A []) {for (INT I = 0; I <. length; I ++) {system. out. print (A [I] + "");} system. out. println ();} public static void main (string [] ARGs) {// The custom object references a A = new (); A B = Changea (a); system. out. println (a); system. out. println (B); system. out. println ("----------------------"); // The String object passed as a parameter is also a reference (only the value of the string object cannot be changed, each value of the string object is modified. A new String object is created to save the modified value. The original value is not changed.) string str1 = "huhualiang"; system. out. println (str1.hashcode (); string str2 = changestring (str1); system. out. println (str2.hashcode (); system. out. println (str1); system. out. println (str2); system. out. println ("----------------------"); // the basic type is to pass the int INTA = 8; int intb = changeint (INTA); system. out. println (INTA); system. out. println (intb); system. out. println ("----------------------"); // the basic type of wrapper set is passed as a parameter instead of referencing integer c = new INTEGER (1); integer d = changeinteger (C ); system. out. println (c); system. out. println (d); system. out. println ("----------------------"); // The array is passed to reference int [] arraya = {,}; int [] arrayb = changeintarray (arraya); printarray (arraya ); printarray (arrayb );}}
Running result:
A = 2, B = B
A = 2, B = B
----------------------
711139030
711139030
226046678
226046678
Huhualiang
Huhualiang
----------------------
8
9
----------------------
1
9
----------------------
10 1 2 3
10 1 2 3