• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

increment operator ++

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 21
Eclipse IDE Python Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks.. Things are much clearer now.
Especially, the question by Haque, which triggered the detailed clarification..
Cheers
Anupreet
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic