Page 1 of 1

About Question enthuware.ocajp.i.v7.2.946 :

Posted: Sun Mar 30, 2014 7:02 am
by Nisim123
The enthuware explanation to that question is:
"Arrays are proper objects (i.e. iArr instanceof Object returns true) and Object references are passed by value (so effectively, it seems as though objects are being passed by reference). So the value of reference of iArr is passed to the method incr(int[] i); This method changes the actual value of the int element at 0."

So it keeps me wondering why int i, when sent to the method:
public static void incr(int n ) { n++ ; },
does not increment as well??? :?

Re: About Question enthuware.ocajp.i.v7.2.946 :

Posted: Sun Mar 30, 2014 7:48 am
by admin
Because i is not an Object. It is a primitive.

Re: About Question enthuware.ocajp.i.v7.2.946 :

Posted: Mon Aug 25, 2014 8:14 am
by lorraine
I think I'm missing something here. This evaluates to primitive integer, n[0]++ so it should be treated as normal incrementing of an int. To have it incremented, shouldn't it be something like n[0]+=1 or n[0] = n[0]++;

Code: Select all

    public static void incr(int[ ] n ) { n [ 0 ]++ ; }
Hope you can elaborate more, thanks!

Re: About Question enthuware.ocajp.i.v7.2.946 :

Posted: Mon Aug 25, 2014 10:35 am
by admin
lorraine wrote:I think I'm missing something here. This evaluates to primitive integer, n[0]++ so it should be treated as normal incrementing of an int. To have it incremented, shouldn't it be something like n[0]+=1 or n[0] = n[0]++;

Code: Select all

    public static void incr(int[ ] n ) { n [ 0 ]++ ; }
Hope you can elaborate more, thanks!
n[0]++ is indeed like a normal incrementing of an int. For example, i++; is same as i = i+1; Similarly n[0]++; is same as n[0] = n[0] + 1;
I am not sure what is the confusion.
-Paul.