Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does '+=' mean again?

 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember.
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a += b;
means
a = a+b;
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Bogadi:
a += b;
means
a = a+b;


No it does not!

See

JLS 3 15.26.2
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1057
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic