Is there a difference between these two lines? (Aside from the fact that they are indeed different) :afrog:
Code:x += 1;
x =+ 1;
Printable View
Is there a difference between these two lines? (Aside from the fact that they are indeed different) :afrog:
Code:x += 1;
x =+ 1;
I'm not a JAva person, but I do know that in C they are are different. If it works the same, I think that the first one will inc the variable by one. The second one, will assign 1 to the vairable first, then increment it (to 2). It would be the same as x = 2;
I *think* that's how it works. I could be wrong, but from my days of C, that sounds about right. It all has to do with when the variable is assigned it's value, and when it's incremented (or decremented). The order of the operators determines the order in which that happens.
Tg
??? :confused: ???
As far as I know, there is nothing called =+. I looked it up, and in my book it said nothing about it. And when I just tested it, it gave me this (in netbeans):
------------
myVar = 5
m = 5
------------
myVar += m (gives myVar = 10)
myVar =+ m (gives myVar = 5, it is treated as = (+m))
myVar =- m (gives myVar = -5, it is treated as = (-m))
-----------
What techgnome is talking about in C, is for ++x vs x++, where in the latter one is usualy slower then the first one. You will probably don't see that performance penalty for normal datatypes, but if you use it on classes, then the over head is getting bigger. The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.
I thought so. Its been a while since I've done anything in Java, and this quack instructor I have made mention of it.Quote:
Originally Posted by NoteMe
Thanks
No problemo, mr. Just glad to help out. Hope to see you around more here now...;)
ØØ
I thought I was talking out my arse.... thanks for confirming that for me. :bigyello:Quote:
Originally Posted by NoteMe
Tg
Quote:
Originally Posted by techgnome
Nhaaa...you where pretty much right about it...:)
ØØ
I don't know about one being faster than the other, but the real concern is when x is incremented in the equation.Quote:
Originally Posted by NoteMe
++x - x is incremented and then used in the equation
x++ - x is used in the equation and then incremented
So, if
x = 10;
then
y = ++x; // end results; x = 11, y = 11
whereas
y = x++; // end results; x = 11, y = 10
Quote:
Originally Posted by ccoder
I am right....;)...tell me if you need proof....;)
+= is a valid operator but =+ is not but i guess the compiler treats =+ as = +, just putting the sign to the integer.
Yeah, I didn't think it was valid as he was telling us, but its been a while, so I wasn't sure. The Format button in the IDE cleared that one up. Oh, and what's his face, too. :afrog:Quote:
Originally Posted by Dilenger4
Sorry I had to drag this one up again. But blame Mendhak, it was he that asked me, and I am not sure if I understand it...:)
Code:
i = 0
i = i++;
//now i = 0
Can anyone explain that? Shouldn't that equal:
They are referencing the same place in memory. I don't get it...Code:i = 0
i = i
i++;
//now i = 1
Of course this is a dummy question, because no one would do it (unless they like to write confusing code). But I still don't get why i isn't 1....grrr...
ØØ
In C, this is a classical code snippet. By the C language specification, it yields undefined behaviour. I don't know about Java.
However, I can tell you how you arrive at i == 0.
i -> 0
> i = i++;
execute right side
i -> 1, expr -> 0
execute assignment
i -> 0
I must admitt that I didn't understand that..:D...w00t...is the expression i = i executed before i++ and then after ...ehhh no, that doesn't seem right either...no matter what order I do this in, it ends up to be 1...
In this line:
i = i++;
You first increment i by one, so that i now contains 1. But the result of i++ is still 0, so by replacing the expression on the right side by its result, you get
i = 0;
i is 1 by now, but now the reduced statement is executed, and the 1 is replaced by the value assigned, which is 0.
But if you think that's complicated, try this:
i = i++ + ++i;
This line is truly undefined, by the way. A compiler may reject it.
Quote:
Originally Posted by CornedBee
I don't even want to think about the last one...:)...but I still don't get why i++ is executed before i = i, i++ is a postfix operator, and are supposed to be executed after the increment. But I can settle with it, as long as I know it. It is a stupid pease of code anyway..:)
There's no i = i there! It's as simple as that.
= has a lower precedence than ++. The code is parsed as
i = (i++);
Does that help?
a bit, but how does then this work:
i = 0;
k = 0;
i = k++;
//now i = 0 and k = 1
as in post number 8 here?
i = k++; State: i = 0, k = 0
-> operator precedence
i = (k++); State: i = 0, k = 0
-> evaluate (k++). k is incremented by one, the result of the expression is k's old value
i = 0; State: i = 0, k = 1
-> evaluate (i = 0).
; State: i = 0, k = 1
Ahha....now I totaly get it..:)...hehe...some things doesn't work too well in my brain, but I got it now. Will rate it when I get my spread covered...:)
Thanks a bunch for beeing so patient with an old lady..:)
- ØØ -