• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

increment operator

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ranch{
public static void main(String argds[])
{
int j=0,k=0;
j=j++;
System.out.println(j);
k = j++;
System.out.println(j);
System.out.println(k);
}
}
Compiling and running the code i get the answer as 0,1,0
Would like to know how this happens. What is the use of post increment in real situation.
Thanks for your replies.
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sankar sivassoupramanime:


<pre>
class ranch
{
public static void main(String args[])
{
int j = 0, k = 0;
j = j++ ;
</pre>
Java specifies left-to-right operand evaluation and the order of everything else, too, such as:
The left operand is evaluated before the right operand of a binary operator. This is true even for the assignment operator, which must evaluate the left operand (where the result will be stored) fully before starting on the right operand (what the result is).
Therefore j is set to zero before the j on the right-hand-side is incremented in memory.
<pre>
System.out.println( j );
</pre>
Here you are getting the officially stored j, not the reserved changed one.
<pre>
k = j++ ;
</pre>
k is set to zero before j is incremented.
<pre>
System.out.println( j );
</pre>
Now the incremented j has been stored in j, so you can see that it has been changed to 1.
<pre>
System.out.println( k );
}
}
Compiling and running the code i get the answer as 0,1,0
Would like to know how this happens. What is the use of post increment in real situation.
</pre>

[This message has been edited by Marilyn deQueiroz (edited January 04, 2001).]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of the post-increment operator?
Simply put, it's just a really easy shorthand. Of course, there's it does nothing you couldn't do by explicitly having 'x=x+1' in a following statement, but that doesn't detract from its neatness.
Naturally, you'll see it most in 'for' loops:
for (i=0; i<10; i++)
is of course exactly identical to
for (i=0; i<10; i=i+1)
but the i++ just looks nicer and more concise.
On another practical issue, I once had a multi-dimensional array where I was needed to increment statistics. It was really nice to have:
stat_array[some_dimension][another_dimension][yet_another]++;
If the operator wasn't available, I would have had to write :
stat_array[some_dimension][another_dimension][yet_another] = stat_array[some_dimension][another_dimension][yet_another] +1;
A lot worse looking and (more to the point) more errorprone.
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all it is a good practice to optimize the code wherever possible like using "i++" instead of "i=i+1" for a good performance.It dramatically reduces CPU time.
There are so many ways to optimize the code,some links..
http://www.cs.cmu.edu/~jch/java/size.html http://www.swtech.com/java/optimize/
Anil
 
sankar sivassoupramanime
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marilyn,
Thanks for the info. But explain me the following,
if we say,
case :1
int i =0;
i = ++i;
System.out.println(i);
it gives the result as 1;
refering the book it says that the value of i is incremented and then assigned. Which is true as we get the value 1.
But if we say,
case 2:
int i =0;
i = i++;
System.out.println(i);
it gives the result as 0;
refering the book it says that the value of i is assigned and then incremented. But my question is when the value of i is incremented. I have displayed the value in the next line and it only gives 0.
Tried the same code in language C but the value i get for the second case is 1. Will you please explain this.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this one if
http://www.javacommerce.com/articles/progtips.htm
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For most operations, the JVM use a stack to load the value of the operands (from some memory location) and manipulate the value on the stack, but for the increment operator, it adds directly to the operand.
So for i = i++; First, the value of i(0) is pushed onto the stack, then i is incremented directly so the variable i has a value of 1. Last, when the assignment operator is evaluated, the value on the stack(0) is poped out the stored in i, so i becomes 0 again.
For i = ++i; i is incremented first to becomes 1, then the value of i is pushed onto the stack. When the assignment operator is evaluated, the value on the stack(1) is poped out and stored back in i.
This also explains the original example. After j = j++; j has the value of 0. For k = j++; the value of j(0) is first pushed onto the stack, then j is incremented to become 1. When the assignment is evaluated, the value on the stack (0) is poped out and stored in k, so k is 0.
Beilin
 
sankar sivassoupramanime
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot buddies for helping to clear my doubt!!!
Thanks Beilin!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!