Results 1 to 6 of 6
Thread: Simple code, but confusing
- 01-20-2009, 11:11 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
Simple code, but confusing
Java 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, 12:25 PM #2
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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, 01:15 AM #3
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
i = (i++) + f1(i);
Try that instead
- 01-21-2009, 01:18 AM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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
Have no idea where you take double as 0.1 here ?As per my idea it should give 1,1
Your program works just as it written... what problem you can see in it?Last edited by Webuser; 01-21-2009 at 01:24 AM.
- 01-22-2009, 09:07 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 11
- Rep Power
- 0
Hi, what's the significanc of
return 0;
?
Thanks!
- 01-22-2009, 10:26 AM #6
Inline comments how it process
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);
}}One Life!!! Y Serious??? :)
Similar Threads
-
Simple code. Could you help pls..?
By Iliyas in forum New To JavaReplies: 8Last Post: 12-26-2008, 03:17 AM -
Simple code help
By rednose in forum New To JavaReplies: 11Last Post: 11-30-2008, 06:02 AM -
Please help me this is simple bit of code
By BlitzAcez in forum New To JavaReplies: 4Last Post: 11-27-2008, 05:52 AM -
Confusing with Frameworks
By hisouka in forum New To JavaReplies: 0Last Post: 08-05-2008, 05:52 AM -
simple code
By elizabeth in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks