| Author |
What does '+=' mean again?
|
Lisa Beglaw
Ranch Hand
Joined: Jul 16, 2005
Posts: 65
|
|
|
I can't remember.
|
 |
Mahesh x Bogadi
Ranch Hand
Joined: Jul 06, 2004
Posts: 51
|
|
a += b; means a = a+b;
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Mahesh Bogadi: a += b; means a = a+b;
No it does not! See JLS 3 15.26.2
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Lisa Beglaw
Ranch Hand
Joined: Jul 16, 2005
Posts: 65
|
|
Thanks for the link Barry, but that website isn't very beginner friendly. I finally managed to find it in my class notes... number = number + value number += value Exactly like Mahesh said.
|
 |
Stuart Gray
Ranch Hand
Joined: Apr 21, 2005
Posts: 410
|
|
And be a bit careful because it is quite easy to write: a=+2; instead of: a+=2; The former will assign the value of 2 to a, whilst the latter will increment it by 2.
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 952
|
|
Originally posted by Mahesh Bogadi: a += b; means a = a+b;
Well... that's right most of the time. With a=a+b, the 'a' expression gets evaluated twice, but with a+=b, it's evaluated only once. For something like... the difference is moot. However, for something like... lines 9a and 9b do two different things. 9a calculates the index into arr[] just once, and so is like... But 9b is like...
|
 |
Daniel .J.Hyslop
Ranch Hand
Joined: May 23, 2005
Posts: 55
|
|
hi lisa , I had a problem with a similar problem of identifying the difference between the two.Maybe someone could shine some light on what is exactly happening here.And what the underlying difference is? declare and instatiate two variables: int i = 1; char c = 'c'; c+=i; //this compiles c=c+i; //this does not although both expressions say the same thing ie, add int i to char c and then initialise c to that value,one works and the other doesn`t.I can`t think that it has anything to do with casting types(although I could be wrong)so it must be something to do with the difference between the + operator and the += operator and the way they produce the outcome.Perhaps someone could shine some light on that ?
|
an island in the sun <br />with a language of many tongue?
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by Daniel .j.Hyslop: hi lisa , I had a problem with a similar problem of identifying the difference between the two.Maybe someone could shine some light on what is exactly happening here.And what the underlying difference is? declare and instatiate two variables: int i = 1; char c = 'c'; c+=i; //this compiles c=c+i; //this does not although both expressions say the same thing ie, add int i to char c and then initialise c to that value,one works and the other doesn`t.I can`t think that it has anything to do with casting types(although I could be wrong)so it must be something to do with the difference between the + operator and the += operator and the way they produce the outcome.Perhaps someone could shine some light on that ?
c += i; contains an implicit class, so it is actually similar to c = (char) (c + i); So once again, the two statements given in the original post do NOT quite say the same thing. Layne
|
Java API Documentation
The Java Tutorial
|
 |
 |
|
|
subject: What does '+=' mean again?
|
|
|