• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

weird increment operator behaving more weird when passed to the method

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh yes
thanks prithvi
you help me everytime I get into trouble
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Prasad,

You are most welcome.

Best Wishes,
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic