Does array changes in method?

前端 未结 6 834
一生所求
一生所求 2020-12-10 02:36

When I write like this:

public class test {

    void mainx()
    {
        int fyeah[] = {2, 3, 4};
        smth(fyeah);
        System.out.println(\"x\"+fy         


        
6条回答
  •  醉梦人生
    2020-12-10 03:10

    Seeing it a bit less technically, I would say that

        fyeah[0] = 22;
    

    is changing the content of (the object pointed to by) fyeah, while

        fyeah = 22;
    

    is changing (the variable) fyeah itself.
    Another example:

    void smth(Person person) {
        person.setAge(22);
    }
    

    is changing (the content of) the person while

    void smth(Person person) {
        person = otherPerson;
    }
    

    is changing the variable person - the original instance of Person is still unchanged (and, since Java is pass-by-value, the variable in the calling code is not changed by this method)

提交回复
热议问题