Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Sadashiv Borkar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
You don't like waffles? Well, do you like this tiny ad?
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!