aspose file tools*
The moose likes Beginning Java and the fly likes Unexpected operator sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected operator sequence" Watch "Unexpected operator sequence" New topic
Author

Unexpected operator sequence

Patrick Muls
Greenhorn

Joined: Aug 08, 2001
Posts: 25
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

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
...to fix it, replace (rCnt++) with (++rCnt).
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Or --


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Patrick Muls
Greenhorn

Joined: Aug 08, 2001
Posts: 25
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
I'm saying the parens make a difference when you use binary operators, not a unary one.
Patrick Muls
Greenhorn

Joined: Aug 08, 2001
Posts: 25
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

Joined: Oct 17, 2001
Posts: 4313

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unexpected operator sequence