# Simple code, but confusing

• 01-20-2009, 12:11 PM
t.prasanna
Simple code, but confusing
Code:

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

When the above code compile and run, it give 1,0 (I am confused how?)
when it pass the value its passing as 1 then the value should remain same when it returns zero.
and it should be like i = 1 + 0;

As per my idea it should give 1,1

Can any one explain clearly how it works here ?
• 01-20-2009, 01:25 PM
t.prasanna
I got it how it work, if some one not clear can check the below link:p

coderanch.com/t/244938/Programmer-Certification-SCJP/post-increment-confusion
• 01-21-2009, 02:15 AM
Webuser
i = (i++) + f1(i);
• 01-21-2009, 02:18 AM
Webuser
i = i++ + f1(i);
remember that java support linear algorythm from up to down
it gives 1.0 because
i is 1 here and f1(i); is zero :)
See yourself
Quote:

As per my idea it should give 1,1
Have no idea where you take double as 0.1 here ?
Your program works just as it written... what problem you can see in it?
• 01-22-2009, 10:07 AM
helloworld
Hi, what's the significanc of

return 0;

?
Thanks!
• 01-22-2009, 11:26 AM
Thennarasu

class C{
static int f1(int i) {
//argument i=1
System.out.print(i + ",");
//prints 1,
return 0;
}
public static void main (String[] args) {
//Program starts here
int i = 0;
//i value is 0
i = i++ + f1(i);
/**i++ is evaluated , then i=1 and 1 is passed
*as parameter to f1(),there it prints----> 1,
*then returns 0 to statement i=i++ +f1(i)
*here,i=0+0
*which is equal to 0 and then prints 0
*so finally 1,0
*/

System.out.print(i);
}}