File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes n++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "n++" Watch "n++" New topic
Author

n++

tricktan
Greenhorn

Joined: Jan 26, 2000
Posts: 15
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.

CitySlicker
Greenhorn

Joined: Feb 03, 2000
Posts: 16
++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.

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm moving this to Java in General (Intermediate) where it's more on-topic and can get wider attention.


"I'm not back." - Bill Harding, Twister
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20732
    ∞

"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.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Dai Feilong
Greenhorn

Joined: Oct 31, 2011
Posts: 1
I am confused too.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: n++