Author
why output is 1,1 ?
Amisha Shah
Ranch Hand
Joined: Mar 03, 2006
Posts: 33
posted Mar 15, 2006 07:19:00
0
programm : class Cc{ 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); }} according to me output of this programm is 1,0 . but output is 1,1. how this programm works ?
Amisha Shah.<br />SCJP 1.4
Graham Walsh
Greenhorn
Joined: Mar 08, 2006
Posts: 23
posted March 15, 2006 07:19 AM -------------------------------------------------------------------------------- programm : class Cc{ 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 you call; i = i++ +f(i) you're effectively doing; i = 0++ + f(1) => i = 1 + 0; => i = 1; f(1 ) => outputs "1," and the print statement prints the value of i which, is 1 output 1,1 G
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
When I run the code, the output is 1,0
Graham Walsh
Greenhorn
Joined: Mar 08, 2006
Posts: 23
doh. my fault. i = 0++ + f(1) => i = 1 + 0; => i = 1; is wrong. It should have read; i = 0++ + f(1) => i = 0 + 0; => i = 0; sorry sorry sorry G
Amisha Shah
Ranch Hand
Joined: Mar 03, 2006
Posts: 33
posted Mar 15, 2006 11:15:00
0
you are right. i = 0++ + f(1) => i = 0 + 0; => i = 0; output is 1,0 thanks.
subject: why output is 1,1 ?