|
-
Apr 13th, 2005, 08:15 AM
#1
+= vs =+
Is there a difference between these two lines? (Aside from the fact that they are indeed different) 
Last edited by crptcblade; Apr 13th, 2005 at 11:28 AM.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 13th, 2005, 09:01 AM
#2
Re: += vs =+
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
-
Apr 13th, 2005, 11:00 AM
#3
Re: += vs =+
??? ???
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.
-
Apr 13th, 2005, 11:27 AM
#4
Re: += vs =+
 Originally Posted by NoteMe
???  ???
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
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 13th, 2005, 11:29 AM
#5
Re: += vs =+
No problemo, mr. Just glad to help out. Hope to see you around more here now...
ØØ
-
Apr 13th, 2005, 12:34 PM
#6
Re: += vs =+
 Originally Posted by NoteMe
???  ???
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.
Tg
-
Apr 13th, 2005, 12:47 PM
#7
Re: += vs =+
 Originally Posted by techgnome
I thought I was talking out my arse.... thanks for confirming that for me.
Tg
Nhaaa...you where pretty much right about it...
ØØ
-
Apr 13th, 2005, 01:27 PM
#8
Frenzied Member
Re: += vs =+
 Originally Posted by NoteMe
???  ???
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
-
Apr 13th, 2005, 01:41 PM
#9
Re: += vs =+
 Originally Posted by ccoder
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....
-
Apr 13th, 2005, 05:49 PM
#10
Dazed Member
Re: += vs =+
+= is a valid operator but =+ is not but i guess the compiler treats =+ as = +, just putting the sign to the integer.
-
Apr 13th, 2005, 07:21 PM
#11
Re: += vs =+
 Originally Posted by Dilenger4
+= 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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 29th, 2005, 08:05 AM
#12
Re: += vs =+
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:
Code:
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...
ØØ
-
Jun 29th, 2005, 08:46 AM
#13
Re: += vs =+
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 29th, 2005, 09:10 AM
#14
Re: += vs =+
I must admitt that I didn't understand that.. ...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...
-
Jun 29th, 2005, 04:47 PM
#15
Re: += vs =+
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 29th, 2005, 04:53 PM
#16
Re: += vs =+
 Originally Posted by CornedBee
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..
-
Jun 29th, 2005, 05:19 PM
#17
Re: += vs =+
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 29th, 2005, 05:23 PM
#18
Re: += vs =+
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?
-
Jun 29th, 2005, 05:53 PM
#19
Re: += vs =+
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 30th, 2005, 02:19 AM
#20
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|