Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Streams and post increment operator

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I have a doubt about the OCP 8 Study Book (Boyarsky/Selikoff), chapter 4 question 11:



I would like to know why the result is 11111?
why the post-increment doesn´t work here?
and how the control flow is in this piece of code?

Thank you very much in advance!
 
Saloon Keeper
Posts: 10422
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think the post-increment isn't working? It's doing exactly what it's supposed to do. If x is 1, what's the return value of x++?
 
Javier Gonzalez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we remove the post increment operator the result is the same 11111, that is why question. why the post increment doesn´t increment to get 12345?
 
Rancher
Posts: 3303
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stephan asked: what is the return value of x++?

To put it another way, consider this program:


What does it print?  Why?
 
Marshal
Posts: 65069
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Javier Gonzalez wrote:. . . why the post increment doesn´t increment to get 12345?

Have a look at this FAQ, and see whether that helps you. If not, remember there are two values:-
  • 1: The value of i, which is increased by 1, and
  • 2: The value of i++, which is the same as the old value of i.
  • What you are doing in the λ inside the iterate() call is to take the old value and apply it to its variable again. So you are resetting the variable to its original value, 1. Everyy time you execute i -> i++, you are resetting i to 1; you don't see the new value of i until after the plae where the ++ operation happens.So each
     
    Javier Gonzalez
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much Campbell Ritchie for answer my question and explain the steps to resolve my doubt about the piece of code that I sent!
    I got it now!
    Thank you again!
     
    Campbell Ritchie
    Marshal
    Posts: 65069
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure Remember other people helped you too.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!