• Post Reply Bookmark Topic Watch Topic
  • New Topic

increment operator ++

 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test
{
public static void main(String args[])
{
int i = 1;
i = i++;
System.out.println(i);
}
}
-----------------------------------------------
Output: 1
------------------------------------------------
Any clues on why output is not 2, when i has been incremented ?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupreet Arora:
class Test
{
public static void main(String args[])
{
int i = 1;
i = i++;
System.out.println(i);
}
}
-----------------------------------------------
Output: 1
------------------------------------------------
Any clues on why output is not 2, when i has been incremented ?

Because the postfix operators (++ and --) return the value before incrementing. So the final effect is to dicard the intermediate result of i+1 and set i equal to whatever it was before incrementing. Try the prefix operator instead:
 
Enamul Haque
Greenhorn
Posts: 21
Eclipse IDE Python Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,

i++;
is equavalant to i=i+1;
so
i = i++;
means
i = i = i+1;
isn't it
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Enamul Haque:
so,

i++;
is equavalant to i=i+1;
so
i = i++;
means
i = i = i+1;
isn't it


It's not equivalent. The pre/postfix operators (++ and --) are unary. All of the arithmetic operators (+, -, * etc.) are binary. Binary in the sence that it requires two operands. The statement i=i+1 as you probably already know is programmer shorthand for:
Grab the contents of i
Add 1
Return the sum
Store the sum at i
The term i+1 has absolutetly no effect on i. But the unary operators do affect their operand. i++ means:
Grab the contents of i
Add 1
Store the sum at i
Return i (original value)
For ++i:
Grab the contents of i
Add 1
Store the sum at i
Return the sum
So the effect of i=i++ is to temporarily store i+1 at i but then when it returns the original value, that is stored at i so no final change takes place.
[ June 30, 2003: Message edited by: Michael Morris ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To simplify what Michael is saying, the postfix operator returns the value of i prior to incrementing it. So if i is equal to 0, when you do i=i++ you get this:
A) return 0
B) increment i to be equal to 1
C) take the returned value from step A and assign it to i
You have to remember that the postfix operator has a higher operator precedence than assignment so that is why the order is as above.
 
Ravi Arikere
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another Way of writing your program is
class Test
{
public static void main(String args[])
{
int i = 1;
//i = i++; This line is commented.

System.out.println(i++);
}
}
Since i++ is what is called as a "post- increment" operator, the compiler allows the operation following the increment statement to be performed and then does the incrementing. So in your case, first the present value of "i" which is 1 is displayed and then the increment operation is done.
Hope this helps.
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks.. Things are much clearer now.
Especially, the question by Haque, which triggered the detailed clarification..
Cheers
Anupreet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!