Java, pass-by-value, reference variables

后端 未结 5 1312
离开以前
离开以前 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:43

    The method receives variables by value. Those values can't be changed (as far as the caller of the method sees), but the values contained within them can (if it's an object, or as in this case, an array).

    So when you change the value b[0] inside the array, the change can be seen outside the method. However the line

    c = b;
    

    will change the value of c inside the method, but that change will not be seen outside the method, since the value of c was passed by value.

提交回复
热议问题