• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment operators  RSS feed

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Core Java Fundamentals book by Horstman, page 48.
It is said that there is a difference between ++m and m++ but as you see the result below, both a=b=16.
How is wrong?

book



run:
a=16
b=16
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Ka wrote:Core Java Fundamentals book by Horstman, page 48.
It is said that there is a difference between ++m and m++ but as you see the result below, both a=b=16.
How is wrong?

book



run:
a=16
b=16


Did you noticed that in your example, you incremented m twice -- and didn't increment n at all? Do you think that it could be related to that? ... and BTW, sometimes, in order to see a difference, you have to try out more than one example.

Henry
 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
A Ka wrote:Core Java Fundamentals book by Horstman, page 48.
It is said that there is a difference between ++m and m++ but as you see the result below, both a=b=16.
How is wrong?

book



run:
a=16
b=16


Did you noticed that in your example, you incremented m twice -- and didn't increment n at all? Do you think that it could be related to that? ... and BTW, sometimes, in order to see a difference, you have to try out more than one example.

Henry

oh, my bad. sorry. was a hard working day
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Ka wrote:
oh, my bad. sorry. was a hard working day


No worries... And BTW, welcome to the ranch !!

Henry
 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
A Ka wrote:
oh, my bad. sorry. was a hard working day


No worries... And BTW, welcome to the ranch !!

Henry


Thank you. This forum is awesome!
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sort of example you want to try to see the difference between ++i and i++ is this sort of thing:-I challenge you to predict the output before you run that code Don't read this FAQ until after you have tried the code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!