PDA

Click to See Complete Forum and Search --> : += vs =+


crptcblade
Apr 13th, 2005, 08:15 AM
Is there a difference between these two lines? (Aside from the fact that they are indeed different) :afrog:

x += 1;
x =+ 1;

techgnome
Apr 13th, 2005, 09:01 AM
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

NoteMe
Apr 13th, 2005, 11:00 AM
??? :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.

crptcblade
Apr 13th, 2005, 11:27 AM
??? :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))
-----------

I thought so. Its been a while since I've done anything in Java, and this quack instructor I have made mention of it.

Thanks

NoteMe
Apr 13th, 2005, 11:29 AM
No problemo, mr. Just glad to help out. Hope to see you around more here now...;)


ии

techgnome
Apr 13th, 2005, 12:34 PM
??? :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 I was talking out my arse.... thanks for confirming that for me. :bigyello:

Tg

NoteMe
Apr 13th, 2005, 12:47 PM
I thought I was talking out my arse.... thanks for confirming that for me. :bigyello:

Tg



Nhaaa...you where pretty much right about it...:)



ии

ccoder
Apr 13th, 2005, 01:27 PM
??? :confused: ???
What techgnome is talking about in C, is for ++x vs x++, where in the latter one is usualy slower then the first one.
I don't know about one being faster than the other, but the real concern is when x is incremented in the equation.
++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

NoteMe
Apr 13th, 2005, 01:41 PM
I don't know about one being faster than the other, but the real concern is when x is incremented in the equation.
++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


I am right....;)...tell me if you need proof....;)

Dillinger4
Apr 13th, 2005, 05:49 PM
+= is a valid operator but =+ is not but i guess the compiler treats =+ as = +, just putting the sign to the integer.

crptcblade
Apr 13th, 2005, 07:21 PM
+= 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:

NoteMe
Jun 29th, 2005, 08:05 AM
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...:)




i = 0

i = i++;

//now i = 0




Can anyone explain that? Shouldn't that equal:


i = 0

i = i

i++;


//now i = 1


They are referencing the same place in memory. I don't get it...

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...



ии

CornedBee
Jun 29th, 2005, 08:46 AM
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

NoteMe
Jun 29th, 2005, 09:10 AM
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...

CornedBee
Jun 29th, 2005, 04:47 PM
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.

NoteMe
Jun 29th, 2005, 04:53 PM
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.


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..:)

CornedBee
Jun 29th, 2005, 05:19 PM
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?

NoteMe
Jun 29th, 2005, 05:23 PM
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?

CornedBee
Jun 29th, 2005, 05:53 PM
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

NoteMe
Jun 30th, 2005, 02:19 AM
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..:)



- ии -