This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Unexpected operator sequence

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a small program to unload some DB-table, and to keep track of the progress being made, I added the following line:
if ((rCnt++)%1000 == 0) System.out.println(rCnt+" records unloaded");
to print a message every 1000 records. I would expect to see:
1000 records unloaded
2000 records unloaded
3000 ...
but I get:
1001 records unloaded
2001 records unloaded
3001 ...
I wonder what the exact sequence of the operations is like. I expected that first rCnt++ was done before the % 1000 is executed on the result. But seeing the output I get the impression it's the other way around. Can someone explain me this?
Thanks,
Patrick
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.... so your code looks like this:

On line 1 you have a post increment meaning it first does the % operation, then increments the value. So on line 1 the value of rCnt == 1000 and the % does in fact equal 0. By the time the program execution reaches line 3, rCnt has now been incremented to 1001 (because of the post increment), and the value 1001 is displayed.
[ August 02, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...to fix it, replace (rCnt++) with (++rCnt).
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or --
 
Patrick Muls
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessica,
I would expect the parantheses around the post-incrementor to force it to be executed before the %-operand.
Stu, Marilyn,
Thanks for your solutions, but the main question is why the parentheses are not taken into consideration for the sequence of operations.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Patrick Muls:
Jessica,
I would expect the parantheses around the post-incrementor to force it to be executed before the %-operand.


It is! It is just that the value of (x++) is the value of x *before* the increment.
That is a common misunderstanding: There is no difference between ++x and x++ regarding when the operation gets executed (evaluated). Both increment x at the same time relative to the surrounding expression. The only difference is in what value the rest of the expression sees.
Does that make it any clearer?
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int rCnt = 1000 ;
if (( rCnt++ ) % 1000 == 0) System.out.println(rCnt+" records unloaded");

In this case the parens make no difference because you are not enclosing a (binary) operator. Kinda like saying
( -1 ) is different than -1.

Java programming language implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence. JLS 15.7.3

If you want to see exactly what is happening, run this simple program (after you compile it) with javap -c TestIncrement with and without the parens.


[ August 05, 2002: Message edited by: Marilyn de Queiroz ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marilyn de Queiroz:
In this case the parens make no difference because you are not enclosing an operator. Kinda like saying ( -1 ) is different than -1.


I don't understand what you want to say with this. Certainly "-" is an (unary) operator in your example above, as is "++" in the original posting, isn't it?
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying the parens make a difference when you use binary operators, not a unary one.
 
Patrick Muls
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your input. In the future I'll try not to be a smartass and do it over 2 lines, first the increment, and then the division, so I'll understand exactly what is going to happen
Patrick
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahhh... and there's one of the rules that I learned at JavaOne from Josh Bloch's "Programming Puzzler's" session
Don't write overly clever code -- you'll probably get hosed.
(well, he didn't say "hosed"... but you get the idea)
Check out the session slides, they're pretty straight forward and have a lot of good examples.
[ August 06, 2002: Message edited by: Jessica Sant ]
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!