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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

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

Post 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??? :?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Because i is not an Object. It is a primitive.

lorraine
Posts: 3
Joined: Tue Nov 26, 2013 11:03 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests