aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why output is 1,1 ? 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 "why output is 1,1 ?" Watch "why output is 1,1 ?" New topic
Author

why output is 1,1 ?

Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
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: 2367
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
you are right.

i = 0++ + f(1)
=> i = 0 + 0;
=> i = 0;

output is 1,0

thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why output is 1,1 ?