File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected operator sequence

 
Patrick Muls
Greenhorn
Posts: 25
  • 0
  • 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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • 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 ]
 
Stu Glassman
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...to fix it, replace (rCnt++) with (++rCnt).
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or --
 
Patrick Muls
Greenhorn
Posts: 25
  • 0
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • 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: 9059
12
  • 0
  • 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
Sheriff
Posts: 14112
  • 0
  • 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: 9059
12
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic