This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

n++

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to my understanding :
n++ == ++n
but
(y=x++) != (y=++x)
---- and -----
operanDS are evaluated right to left
while
while operationS are evaluated from left to right
---------------
how come :
int N = 2;
int method(int N){
return 0X100 + N++;
}

yeilds 258 ??
Shouldn't N be 3 before added to 0X100 ?
Thanks for input.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
++n adds 1 to n FIRST and uses the new value of n in an expression, while n++ uses the value of n in the expression, then
adds 1. So in your method, the expression "return 0X100 + N++"
N is 2.

Originally posted by tricktan:
to my understanding :
n++ == ++n
but
(y=x++) != (y=++x)
---- and -----
operanDS are evaluated right to left
while
while operationS are evaluated from left to right
---------------
how come :
int N = 2;
int method(int N){
return 0X100 + N++;
}

yeilds 258 ??
Shouldn't N be 3 before added to 0X100 ?
Thanks for input.


 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Java in General (Intermediate) where it's more on-topic and can get wider attention.
 
Trailboss
Posts: 23216
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"CitySlicker" is correct. There is a difference between the prefix incrment (++n) and the postfix increment (m++). And it has to do with when the increment happens.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused too.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dai Feilong, welcome to the Ranch!

Please note that the post that you replied to is more than 11 years old! The person who asked the question is probably not still waiting for an answer...
 
Marshal
Posts: 69882
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

paul wheaton wrote:"CitySlicker" is correct. . . .

No, he is mistaken.

The truth is that there are two values, the value of n, which you can’t see, and the value of n++, which is the same as the old value of n.Try searching for “preincrement postincrement” on this forum and the “Beginning Java” forum, because this question and similar ones arise at least once a month. You can’t search for n++ unfortunately.
 
    Bookmark Topic Watch Topic
  • New Topic