weird increment operator behaving more weird when passed to the method

first of all we all know that when we are passing the primitive variable to a method then we are passing a copy of the value isn't it?
like this in the following program

this prints out
5
6
5
isnt it?

Now let us take a look at another code

forget about the commented lines right now
what I want to say is
here we are passing variables to the method , but we are using the increment operator on it , when I run the code and see the output, we see that the original value of the variable is changed and not the value of the copy we are passing
that is when we run this code
we get the output as
1,2,2,
that is the the original value of the variables is changing

is this all because we are passing "i++" to the method?
is this the only reason that is changing the original value of the method?

I am finding it very interesting to deal with the increment operators
from this question I also came to know that when we pass with increment , the increment happens only after all the method execution has completed
please experts look into this and tell me the answers..
Prithvi and Larry, I am expecting your answers right now

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right

Actually, i believe . Lets go through it step by step. Here is your code.

When this block is executed, following things happen. method m is called with a post increment operator, so right now the statement
is not completed as it has called a method, the copy of i is passed to m and after receiving the copy, the value of i is printed, after the
return statement this whole statement is completed.

Now i has a value of 2 inside the main() method as the statement has been completed as it is printed out. Remember this m(i++); will
actually complete when the method m is exited.

Again, see the next piece of code

Same process is happening. But this time rather then the value 1 being passed, this time i value is 2 which is passed to the method.
Because previously value of i was incremented from 1 to 2. Here also only the copy is passed and printed. If you slightly modify it
like and also put a println statement after the last call to m(), this time you will see, inside main the value of i which will be printed is
3, because post increment value is only incremented and used once only the whole statement is executed and that is the control is
returned from the method.

Well, yes you are right, the complete execution of the statement like this m(i++) will only happen once the control comes back.

Prasad Khakar wrote:...from this question I also came to know that when we pass with increment , the increment happens only after all the method execution has completed

It is very late at night in the U.S. right now so I have little time to give a big explanation. Your statement quoted above, would be more correct as "...the post-increment happens only after all the method execution has completed" because the code is using i++. As an experiment, change lines 9, 10, 11 in your code to use pre-increment (++i) and compare the different output result:

SCJP 6

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

posted Apr 26, 2010 21:25:56

0

Larry is absolutely correct as i explained the same thing above. Changing it to pre-increment will give you the output as

maybe I forgot to ask another question
isn't it that we are passing the COPY to the method
how come the original variable i is changing even if we are passing a copy to the variable?
or in this case as we are passing it with the increment operator, copy is not passed any original variable is passed?
what is that?
please explain
Good night Larry thank you for helping

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

posted Apr 26, 2010 22:30:03

0

Prasad,

It's the only copy we are passing, we are not passing any original variable.
Specify what makes you think, the original variable is getting passed.

isn't it that we are passing the COPY to the method
how come the original variable i is changing even if we are passing a copy to the variable?
or in this case as we are passing it with the increment operator, copy is not passed any original variable is passed?
what is that?

Because you are incr/decr in original variable and than passing to function

this explains a lot to me
means
we are passing the copy of the variable but when passing the copy we are incrementing the original variable
and when we are using the copy of the variable in the method then the copy is getting printed
and when we come out of the method and then the original variable value is changed
means
the i in the method and the variable i outer to the method m() are completely different
as Ritesh has said, the value of the original variable is changed after the method has called and meanwhile , the copy of the variable was used in the method
am I right now?
thank you prithvi and ritesh for helping

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774

posted Apr 26, 2010 23:35:08

0

Yes Prasad. That's what exactly i explained in my first post to you. That copy will be passed and once the method
returns value is incremented and printed.

In your code,You post increment the value which is in the main not in the method m().So only the original value get changed not a copy.
But in this coe the value which is in the method m() only post incremented so you got the answer like 2,1,2.