Java, pass-by-value, reference variables

后端 未结 5 1334
离开以前
离开以前 2020-11-27 07:50

I have a problem with understanding the \"pass-by-value\" action of Java in the following example:

public class Numbers {

    static int[] s_ccc = {7};
             


        
5条回答
  •  长情又很酷
    2020-11-27 08:52

    On entry to calculate(), b points at s_ccc and c points at t_ccc. Altering b[0] will therefore change s_ccc, as you've seen.

    However, the assignment

    c = b;
    

    only points c at the same object b is pointing at, namely s_ccc. It does not copy the contents of what b points to to what c points to. As a result, t_ccc is unchanged. If you added the following line to the end of calculate():

    c[0] = c[0] + 5;
    

    s_ccc[0] would be 21.

提交回复
热议问题