aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes weird increment operator behaving more weird when passed to the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "weird increment operator behaving more weird when passed to the method " Watch "weird increment operator behaving more weird when passed to the method " New topic
Author

weird increment operator behaving more weird when passed to the method

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1


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
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

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.

Good Morning and Happy Preparation




Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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
Larry is absolutely correct as i explained the same thing above. Changing it to pre-increment will give you the output as

2,2,3

Means immediately incremented and passed.

Hope this helps,
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

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

Best Regards,
Ritesh Pareek
Ranch Hand

Joined: Nov 04, 2008
Posts: 50
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

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Ritesh Pareek wrote:
# m(i++); // Passing variable to m() and getting back over here and increment


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

You are welcome.

Happy preparation,
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Ohh yes
thanks prithvi
you help me everytime I get into trouble
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Prasad,

You are most welcome.

Best Wishes,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prasad,
Try to work out this,


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.


I hope it helps you to understand your answer.


Cheers Munees
My Blog
 
 
subject: weird increment operator behaving more weird when passed to the method