About the reference in parameters

xiaoxiao2021-03-06  42

Public class test {public static void main (string [] args) {stringbuffer a = new stringbuffer ("a"); StringBuffer B = New StringBuffer ("b"); Operate (a, b); system.out.println "A =" A "," "b =" b); b = a; system.out.println ("a =" a "," "b =" b); int i = 7 ; Int J = 9; Operate2 (i, j); system.out.println ("i =" i "," "j =" j); StringBuffer D = New StringBuffer ("Hello"); StringBuffer E = D; E.Append (", world"); system.out.println ("D IS:" D); System.out.Println ("E IS: E);

} Public static void Operate (StringBuffer X, StringBuffer Y) {//y.append ("t "); x.append (y); y = x; //y.append ("0"); system.out. Println ("x =" x "," "y =" y); // return;} public static void Operate2 (int M, int N) {n = m; system.out.println ("m = " m ", " " n = " n);}}}}}} output: x = ab, y = aba = ab, b = ba = ab, b = ABM = 7, n = 7i = 7, j = 9d IS: Hello, Worlde IS: Hello, World // What is the output? // This answer is ab, b // I don't know why B's output or B? Explanation: / * In fact, the set of parameters of the Java object pool is a reference to the same object is a copy, but this copy also points to the original object. = Operation is just a newly generated reference pointing to the original object If the original object reference points to the point, there is no change. In fact, if you understand the Java object pool, it will better understand * /

转载请注明原文地址:https://www.9cbs.com/read-57805.html

New Post(0)