Why does a void function return a value?

前端 未结 5 2007
北海茫月
北海茫月 2021-01-27 07:54

I\'m a programming beginner and I have question regarding a return value from a function.

I´m studying Java.

I have attached code from my book that features a cl

5条回答
  •  心在旅途
    2021-01-27 08:25

    Since arrays are objects, they are passed by their reference (their location in memory), so the changes within sort() to a[] also change a[] declared in main. So a is changed within the function. However, you cannot say

    public static void change(int[] a) {
        a = new int[3];
        a = {1, 2};
    }
    

    That will not change a itself, because that just makes a new memory location that the parameter a points to, without changing the parameter.

提交回复
热议问题