aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the follow of this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the follow of this code?" Watch "What is the follow of this code?" New topic
Author

What is the follow of this code?

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
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);
}}

The answer is 1,0
What is the follow of this code?
abhishek pendkay
Ranch Hand

Joined: Jan 01, 2007
Posts: 184
your question is not clear... the answer 1,0 is correct what is it that you want to know about the program...??


The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
hi tony,

following is the sequence of execution for

int i = 0;
i = i++ + f1(i);


i++ --> I value is substituted for expresseion (0), and i value is incremented . so i value is 1.

f1(i) -- > f1(1) is passed , which is returning 0.

but i = 0 + 0 . so the result is i =0 will be printed.
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
i = 0
i = i++ + f1(i) is evaluated as:
i = 0 + f1(i) //i value(0) is used first. And then i is incremented to 1
i = 0 + f1(1) // i = 1 here.
f1(1) function is called. So 1 is printed. and 0 is returned.
now i = 0 + 0(returned value from f1(int) function)
so, i = 0
Finally you are printing the value of i which is 0.

i hope i explained clearly..
rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Hello kesava narayana
1st i wanna thanks ,cause really i understand that program follow thor your explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the follow of this code?
 
Similar Threads
why output is 1,1 ?
order required
Prime Factor
Why the result is 1,0
Why is this printing 1,0 instead of 1,1