简体   繁体   中英

Java: void method returns values?

So, I think I misunderstood something about how method returns values. I don't understand why list[0] is 3 in the output since that is a void method, which doesn't return anything back to main method... If the void method could actually return the value, then why num would still be 0..... wouldn't num become 3 as well?? Or void method doesn't return any values, except arrays?

public static void main (String []args){

    int []list = {1,2,3,4,5};
    int number = 0;

    modify(number, list);

    System.out.println("number is: "+number);

    for (int i = 0; i < list.length; i++)
    {
        System.out.print(list[i]+" ");
    }

    System.out.println();
}
public static void modify (int num, int []list){

    num = 3;
    list[0] = 3;
}

You likely need to familiarize your self with concepts of "pass by value", "pass by reference", and the understanding that objects are references (passed by value between method calls).

"number" is a simple integer - passed by value to the modify method. So even if the value assigned to "num" changes within the method, the original variable used by the caller of this method remains the same.

"list" is also passed by value, but "list" is an object, so you are passing the reference to this object (by value) to the modify method. So if you change the internals of an object within a method, you are changing the same object referenced by the caller.

Now if you did this:

public static void modify (int num, int [] list)
{

    num = 3;
    int [] newlist = {9,8,7,6,5,4};
    list = newlist;
    newlist[0] = 3;
}

Then the "list" that was passed into modify never gets modified because "list" within the modify method gets changed to point to a different object altogether.

它不返回任何内容,只是修改了变量list[0]

As mentioned above, the method isn't actually returning a value... it is modifying the first value in the array. The output is a reflection of the value modified by the 'modify'

list[0] = 3; is why you gave output 3. It has nothing to do with num and you did not return anything. You simply modified the content of the array.

When you declared "num" in the method, that is simply a new integer that has nothing to do with "number." Yes, you passed "number" into "num" in the method call, but you did not pass back "num" into "number," because in order to do that you would have had to return "num" specifically to the method call, which didn't happen because the return type was void. However, on the other hand, list[0] = 3 was in fact a successful modification to the array because it's not required for the array contents to be returned to the original method caller. Both the main method and the modify method already point to the same array in memory. But on the other hand, it's not clear from the computer's point of view that "num" and "number" point to the same thing in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM