why does this particular program print me five?

Mr Miyagi :

Please help me with this one guys! why are there arguments given to a method that is void??

public class C {   
    public static void main(String[] argv) {     
        int k = 1;     
        int[] x = {0, 1, 2, 3};     
        int[] y = x;     
        lurig(x, y, k);     
        System.out.println(x[0] + k + y[0]);   
    } 

    public static void lurig(int[] p, int[] q, int r) {     
        p[0] = 1;     
        q[0] = 2;     
        r = 3;   
    } 
} 
InsertKnowledge :

x[0] is 0 k is 1 y[0] is 0

-> lurig() changes x[0] to 1 and y[0] to 2 but since y has the same reference as x it also changes x[0] to 2 k is a primitive type so it remains as 1 (completely unaffected)

-> 2 + 1 + 2 gives 5

5 is NOT returned it's being written out to the console. You don't have a return statement you have a System.out out stands for output.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334669&siteId=1