• 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

How the increment work

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one explain me the out put of following program

class Class1{
static int f1(int i) {
System.out.print( i + "," );
return 0;
}
public static void main (String[] args) {
int i = 0;
System.out.print( i + "," );
i = i++ + f1(i);
System.out.print(i);
}
}

When I run this I get 0,1,0 as the output. I dont understand how we get 1 and 0 as last 2 digits. It is confusing.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "Ruwan Ruwan", welcome to JavaRanch. Please check your private messages (see the link at the top right of the page) for an administrative JavaRanch matter.

Have a look at PostIncrementOperatorAndAssignment in our FAQ.
 
Ruwan Scott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jasper,
I already read it. I couldnt relate that logic to this piece of code. problem is what happens inside method f1. we pass 0 to that method , but inside the method it is printed as 1. that is what confuse me.
Thanks.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruwan please Use Code Tags when you post a source code.



In this statement, when i++ is executed, the original value of i i.e. 0 is taken in the expression and then i is incremented to 1. So the statement would look as



But as I said, the value of i is incremented after the original value is used in the expression, so when f1 is called, the value of i is 1. Thus 1 is displayed as the second last digit in the output. The method f1 returns 0 so the expression becomes



And thus the last digit in the output is 0 as the value of i becomes 0 after this statement is executed...
 
Ruwan Scott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
thanks for explanation. Now i know how f1 method prints 1.
I have a small quesiton here . when compiler evaluates i++ + f1(i) part, Why doesn't it copy 0 to i++ and f1(i) at the same time.
Is it evaluates from Left to Right ? Can you explain this?

Thanks
Ruwan.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see the expression, there is a binary operator in it i.e. the + operator. The operands of the + operator are evaluated from left to right that's why 0 is not put in the call to f1(). It will first completely execute the left operand and then execute the right operand. Try this to clear your doubt

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

Ankit's old explanation was good but the last one with the code example is not accurate as the last output will be 3 instead of 12, the reason why the previous output is printing 12 is because you are printing the string literals "1" and "2" respectively.

Now back to the original question, i++ is a post increment which means that i=0 is saved first then i++=1 is used to call the f() method which returns 0 ofcourse. Now the old saved i=0 is used + 0 from the returned function which yield to 0.

Try i = ++i + f1(i); and you will see the difference because i will be incremented first and saved as 1 then used in the method call, the final i result should be 1.

Cheers!!!
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Jay wrote:Ankit's old explanation was good but the last one with the code example is not accurate as the last output will be 3 instead of 12,



What do you mean by the last output. I never displayed the value of i so 3 will not be in the output...
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The value of i will print 3 and not 12 as you commented in your code above.


 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Jay wrote:

The value of i will print 3 and not 12 as you commented in your code above.



I think you misinterpreted it or I didn't explain it properly. When I said output 12, I didn't mean that 12 will be assigned to i. I meant to say that the output of execution of that expression will be 12...
 
Ruwan Scott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit , Mo
Thanks for your explanations. That clears all my doubts on this issue.

/Ruwan
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic