0
Chivo

i=i++?

Recommended Posts

I've been studying to get my first two levels of certification (Java Programmer & Java Developer). Its interesting to learn the behavior of the language, specially in trivial things like...

int i = 0;
i = i++;
System.out.println("" + i);

What do you think the result would be? 0 or 1? Funny how many ppl get this wrong... but shouldn't this be trivial?

The good news is that this behavior is expected and documented. Thats the only way Sun can make sure that their Virtual Machine works the same way in all platforms.

What a pain!!! And this is one of the many many little quirks I've come across.

Yes... I'd rather be skydiving!

~Chivo

Share this post


Link to post
Share on other sites
Quote

The good news is that this behavior is expected and documented. Thats the only way Sun can make sure that their Virtual Machine works the same way in all platforms.

What a pain!!! And this is one of the many many little quirks I've come across.


It doesn't look like a quirk to me. You are using a postfix increment, where the value of the variable is evaluated before it is incremented (as opposed to a prefix increment, like ++i). It is the way the operator is designed to work, and the way that it does work in every programming language that I've ever encountered.

Share this post


Link to post
Share on other sites

Ok, its not a quirk but I've found a LOT of Java and C/C++ developers that expect a different result. The problem relies on how ppl expect certain behavior without really understanding the language. Some think that the ++ is evaluated after the expression, other think its immediately after the i++, and others think its before the assignment.

int i = 1;
i = i++ + i++ + i++;
What should the value of i be?

And it gets worse when there's optimization... and short circuiting... like...

if (false && (i++ > 0)) {
}

since the expression is always false, it should short circuit the (i++ > 0), so the i++ would never be executed. :S

Anyway, if I ever see a programmer writing code like... i=i++ or any of the other examples I put here... I would KICK HIS AZZ!!!!

~Chivo

Share this post


Link to post
Share on other sites
I agree that this is a post fix operator and the operation should happen after the assignment but wouldn't the value of i still be 1 in this case because you assign to i then increment i.

I agree if you did something like
int i=0;
x=i++
then z would be 0 ; but after the operation i=1
so shouldn't
i=i++
still be equal to one
I think I confused myself

Edited to say I'm a nerd an went a wrote the program real quick and it does infact output 1, at least in C++

B.L. (the brain)

Share this post


Link to post
Share on other sites
Quote

Otherwise you'd have to do

X=Y
Y=++Y



Actually... 80% of all software dollars go to maintenance. So, if writing it in 2 lines instead of one makes it a lot clearer, then I vote for 2 lines. The compiler doesn't care, but the next 14 people who maintain the code will.

The original code would best be written as:
i++;

That solves the ambiguity problem. I spend half my time decipering something that some genius wrote and when I pass it back to them, they say "WTF???".

I always tell entry level people that I want clarity and plenty of comments if they wish to be promoted. If they write nasty code, they get to maintain it for the rest of their lives. The choice is theirs.

Share this post


Link to post
Share on other sites
Here's the dissasembly of the pertinent sections of the java class file (comments are mine):

0 iconst_0 // push 0 on to the stack
1 istore_1 // pop top of stack (0) into our local variable i
2 iload_1 // push integer from local variable i onto the stack (so 0 is on top of the stack)
3 iinc 1 1 // increment local variable i by one (note that 0 is still on top of the stack)
6 istore_1 // pop the top of the stack (0) into our local variable i


Edit: It works (prints 0) in Perl: perl -e '$i = 0; $i = $i++; print $i'

Share this post


Link to post
Share on other sites

Man, when programmers pull stunts like this - the code review for them is going to be a case of the Spanish Inquisition.
Because people are prone to getting this wrong on reading it is exactly why one does not use constructs like this without a very good reason - and 99% of the time, the reason is the wrong one anyways.

I remember one of the old-timer ('hack n'slash') S/E's would comment 'I don't trust the compiler so I unit test all of my modules by embedding test code in the release software'. The reply was, "If I used 5 layers of nested #ifdef macros to build union data structures like you do, I wouldn't trust the compiler either - I'd be surprised if build didn't crash outright'.

Just because the language supports funky hard for humans to read operations does not mean we should use them - post-fix and pre-fix operations can become a can of worms quickly for maintence.

As they say KISS.
Simplicity is far harder to achieve than complexity.

Share this post


Link to post
Share on other sites
I agree... this examples were just to show that many ppl expect a different behavior from the language.

There's no need to write production code like that. Bitchslap anyone who does. WITH or WITHOUT comments, code like that should NOT be used in production. NO EXCUSES!

~Chivo

Share this post


Link to post
Share on other sites
That is the problem. C/C++ doesn't define the expected behavior for that scenario. If you try that same C++ code in a different compiler the result might not be the same.

A little confusing isn't it? Specially when you have been programming both in C/C++ and Java.

~Chivo

Share this post


Link to post
Share on other sites
Quote


int i = 0;
i = i++;
System.out.println("" + i);

What do you think the result would be? ~Chivo




Lets have some fun, what does this one print out?

#include "stdafx.h"
#include
#include
#include
char format[] = "%s%s\n\n";
char print[] = "print ";
char result[3];
int i=0;


int main(int argc, char* argv[])
{
_itoa(++i, result, 10);
_asm
{
mov eax, offset result;
push eax;
mov eax, offset print;
push eax;
xor eax, eax;
mov eax, offset i;
inc [eax];
mov eax, offset format;
push eax;
call printf;
pop eax;
pop eax;
pop eax;
}
return 0;
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0