Chivo 0 #1 May 12, 2003 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 Quote Share this post Link to post Share on other sites
Viking 0 #2 May 12, 2003 BOOBIES!!!!!!! I swear you must have footprints on the back of your helmet - chicagoskydiver My God has a bigger dick than your god -George Carlin Quote Share this post Link to post Share on other sites
kevin922 0 #3 May 12, 2003 i'm not a programmer but to me that would be 1 if i=0 i++ means next number after "I" so that would be 1.. but then again like I said i'm not a programmer Quote Share this post Link to post Share on other sites
Muenkel 0 #4 May 12, 2003 What Viking said.Chris _________________________________________ Chris Quote Share this post Link to post Share on other sites
indyz 1 #5 May 12, 2003 QuoteThe 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. Quote Share this post Link to post Share on other sites
PhillyKev 0 #6 May 12, 2003 0 That's a postfix operator so you're setting the value and then incrementing it. When you output it you get the set value before it is incremented. Quote Share this post Link to post Share on other sites
Muenkel 0 #8 May 12, 2003 I bet all this talk is turning Sunshine on.Chris _________________________________________ Chris Quote Share this post Link to post Share on other sites
billvon 3,120 #9 May 12, 2003 int i = 0; Set integer value i to zero. i = i++; Set the new i equal to the old i, then increment the old (not the new) i. System.out.println("" + i); Prints zero since the new i is zero. Quote Share this post Link to post Share on other sites
Chivo 0 #10 May 12, 2003 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. 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 Quote Share this post Link to post Share on other sites
PhillyKev 0 #11 May 12, 2003 Actually it's useful. For example if you wanted to set the value of a variable (X) to the current value of another variable (Y) and then increment Y you would do: X=Y++ Otherwise you'd have to do X=Y Y=++Y Quote Share this post Link to post Share on other sites
freakshow 0 #12 May 12, 2003 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) Quote Share this post Link to post Share on other sites
indyz 1 #13 May 12, 2003 The evaluation happens like this: 1. Get the value of i and store it away somewhere (so, store 0) 2. Increment i (so i equals one now) 3. Assign the value we stored in step one to i (so assign 0 to i). Quote Share this post Link to post Share on other sites
freakshow 0 #14 May 12, 2003 That may be but the following code does output 1 granted it's not java int main() { int i=0; i=i++; cout<return 0; } B.L. (the brain) Quote Share this post Link to post Share on other sites
happythoughts 0 #15 May 12, 2003 QuoteOtherwise 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. Quote Share this post Link to post Share on other sites
indyz 1 #16 May 12, 2003 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' Quote Share this post Link to post Share on other sites
PhillyKev 0 #17 May 12, 2003 I'd rather add a comment line that explains it and write the code more efficiently. It can be just as understandable with good commenting, and comments aren't compiled or processed. Makes for more efficient code. Quote Share this post Link to post Share on other sites
GrumpySmurf 0 #18 May 12, 2003 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. Quote Share this post Link to post Share on other sites
Chivo 0 #19 May 12, 2003 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 Quote Share this post Link to post Share on other sites
Chivo 0 #20 May 12, 2003 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 Quote Share this post Link to post Share on other sites
QuickDraw 0 #21 May 12, 2003 Man...you guys wanna get some b33r down yer necks. -- Hope you don't die. -- I'm fucking winning Quote Share this post Link to post Share on other sites
marcandalysse 0 #22 May 12, 2003 those classes at hcc are paying off Bill! at least in the posts! Quote Share this post Link to post Share on other sites
zonie 0 #23 May 13, 2003 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; } Quote Share this post Link to post Share on other sites
freakshow 0 #24 May 13, 2003 I was already wrong once today so I'm not gonna even try and making a guess on this one B.L. (the brain) Quote Share this post Link to post Share on other sites
freeflir29 0 #25 May 13, 2003 Quote Lets have some fun You're REALLY high right now....right? Quote Share this post Link to post Share on other sites