• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference +=

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


wht is the difference b/w the 2 statements?


a=a+b;
a+=b;



Regards
Suhita
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic