• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Needs clarification in the code execution

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

I have newly joined the team. I am not clear of the execution of the following code, can any one of you help me out,

here is the code,

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

The output is 6,8 astonishingly...

Thanks in Advance,
Siva.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva,

Welcome to JavaRanch!




i is evaluated to 5 for this expression and then incremented by 1 and
becomes 6 so to the method 6 is passed. But for this expression value 5 was
remembered resulting 8 the sum.



Thanks,
 
Siva Pavan Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

Thanks for your warm welcome....

But how can it remember the values of i as 5, while passing the i value as 6 to the method call??....

Can you clarify on this....

Thanks,
Siva.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = i++ + f1(i);


But how can it remember the values of i as 5, while passing the i value as 6 to the method call??....



You need not to remember anything. I told what applies to post increment
operator and such an expression. You may have heard expression evaluation.
First the entire expression is evaluated then operation is done.

i=5; //initially
for this expression i = i++ + f1(i); where you see i++, value of
i++ is evaluated to be 5, and because it is post increment operator the
value is incremented after it is used in the expression. OK? Now when i has
been used in the expression, i is incremented by 1 resulting 6, that is
passed to the method f1(i);

Take care what was the result of evaluating i in the expression we just did?
Correct! It was 5, so it will be used while addition resulting value 8
(5+3(returned from the method f1(...))).


Got it?

Thanks,
 
Don't touch me. And dont' touch this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic