• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference +=  RSS feed

 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


wht is the difference b/w the 2 statements?


a=a+b;
a+=b;



Regards
Suhita
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They both do effectively the same thing, although depending on the data types of "a" and "b", there are cases where the second one will compile and the first will not. They both add a and b, and set a to the result.
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,


i m unable 2 understand u r answer.can u see the following pgm and explain me.




public class Test {

static int a;

int b;

public Test() {
int c = ++a + ++a;
b += c++;
}

public static void main(String[] args) {
System.out.print(new Test().b);
}
}

Thanks in advance


Regards
suhita
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaagh, my eyes! Note that anyone who writes code like this in the real world is in big trouble; you find this sort of silliness in SCJP mock questions, and nowhere else.

That said...

int c = ++a + ++a;[b]
This means c is (++a) + (++a). a starts at zero. The first expression in parentheses equals 1, and the second equals 2, so c becomes 3.

[b]b += c++;

b also starts at zero. We add to it the result of evaluating (c++), which is just the value of c. Here, the "++" happens after the value of c is read. So since c is 3, b also becomes 3.

And that's the output.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR

by default integers are zero, So

int c = 0;
int b = 0;

c = (1) 1 + (2) 2 = 3

b = b + (c++) = 0 + 3 (4) = 3

So b = 3 and c = 3 just like Ernest said.

JC
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,


i satisfied with u r answer.but why they gave b+=c++ instead of b=c++.b=c++ and b+=c++ both r same.i m in little bit confusion?


Suhita
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All that been said the only differente between a=a+b and a+=b is that += autocasts the result.




Hence, when you are using += *= -= or /= operators be careful with precision loss. It is bug hard to find, and your compiler will not even complaint about it.

Regards,
Edwin Dalorzo
[ July 07, 2006: Message edited by: Edwin Dalorzo ]
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks 2 all who gave reponses.Now i understood clearly.



Regards
Suhita
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suhita Reddy:
hi,r.but why they gave b+=c++ instead of b=c++.?


As I said, this is nothing like real code; the whole point of this code is just to be confusing and hard to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!