wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment Operator Imp!!!!!!!!!!!!!!!!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment Operator Imp!!!!!!!!!!!!!!!!!!!!!!!" Watch "Increment Operator Imp!!!!!!!!!!!!!!!!!!!!!!!" New topic
Author

Increment Operator Imp!!!!!!!!!!!!!!!!!!!!!!!

Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
Hi All,
Whats the o/p of the following programme and why
public class Test
{
public static void main(String args[])
{
int i=0;
i = i++;
System.out.println(i);
i = i++;
System.out.println(i);
}
}
If you see above programme the o/p is 1,2 according to me but it gives me 0, 0 as o/p.
can anyone clarify why this is happening.
If you see second programme below ...
public class Test
{
public static void main(String args[])
{
int i=0;
int j = i++;
System.out.println(i);
int k = i++;
System.out.println(i);
}
}
The above programme give the results 1, 2 and not 0, 0 why..?
Please answer Urgently as I will be giving exam very soon...
bye
Sada

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
well, as far as your first program is concerned. The sequence of operation is as follows:
1)take the value i==0; OK
2)now (i++) means first assign i to i and then increment the value, so i is again 0; OK
3)now, first printing will show i=0; OK
4)again, the value of i==0;OK (because the value of i is overwritten with current value 0)
5)now again (i++) means first assign the current value of i to i and then do the increment, so i is again 0; OK
6)finally printing this value of i which shows i as 0; OK
for further clarification see the following:
http://www.javaranch.com/ubb/Forum24/HTML/005742.html
bye,
Tualha Khan


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
in you second program, the only thing noticeable is that, you are NOT assigning the post-incremented values BACK to i. Instead you are assigning them to new integer values. Thereby the value of i is not overwritten with post-incremented value.
The sequence follows:
1)take the value of i==0; OK
2)post-increment it and assign the resultant value to j; OK
3)since you are not overwriting the value of i with the post incremented value, the new value assigned to i is 1; OK
4)printing i to the screen i.e. i==1; OK
5)again take the value of i (which is now 1); OK
6)post-increment it and assign the resultant value to k; OK
7)again, you are not overwriting the value to i itself, but assigning it to a new variable k, hence i preserves it's incremented value, which is now 2; OK
8)printing i on the screen again will give the result as 2; OK
to notice the diferences, print the values of j and k as well.
Bye,
Tualha Khan
Nijeesh Balan
Ranch Hand

Joined: Oct 09, 2000
Posts: 116
Hi,
Just go thru' the discussion link in www.javaranch.com/maha
She has a good collection of discussions on all topics. She has explained increment and decrement operations in detail.
With
Nijeesh.


Thanks & Regards,<br />Nijeesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Increment Operator Imp!!!!!!!!!!!!!!!!!!!!!!!