Java, pass-by-value, reference variables -


I have a problem understanding Java's "pass-by-value" action in the following example:

< Pre> public class number {static inte} [s_ccc = {7}; Fixed int [] t_ccc = {7}; Public static zero counts (int [] b, int [] c) {System.out.println ("s_ccc [0] =" + s_ccc [0]); // 7 System.out.println ("t_ccc [0] =" + t_ccc [0]); // 7b [0] = b [0] + 9; System.out.println ("\ nb [0] =" + b [0]); // 16c = b; System.out.println ("c [0] =" + c [0] + "\ n"); // 16} public static zero main (string [] args) {count (s_ccc, t_ccc); System.out.println ("s_ccc [0] =" + s_ccc [0]); // 16 System.out.println ("T_CCC [0] =" + T_CCC [0]); // 7}} I know that because s_ccc is a reference variable, when i calculate it method and i make some changes in its elements in the method, change my Even after leaving the pass method I think that should be with the same T_CCC. It is again a reference variable, I give it a calculation method, and in this method I change re_fec to t_ccc as s_ccc. Now t_ ccc, a reference variable should be indicated for an array, in which the type of an element is equal to int 16. But when the calculation () is omitted, it seems that T_CCC indicates its old object. Why is this happening? Should not there be any change for this? It is a reference variable after all.

Regards

There is an extended discussion Previous question "How does Java pass through Java" is? "Java actually passes the value to object context.

In your code, the reference to the array (which is the object) is calculate () These references are passed from the value, which means that any change in the values ​​of b and c can be seen within the method (They are actually only S_ccc and t_ccc ). Therefore t_ccc has not affected main ()

] B, the last int [] C)

Now, the compiler will not even allow you to change the values ​​of b or c Of course, the negative side of it is that you can not easily manipulate them in their method.


Comments