This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by tnemani: What is the difference between a+=4 and a=a+4; if we decare a as byte and if we assign a=5 ;i am getting the answer for a+=4; but for a=a+4 i got compilation error why?
except for the leaving the semicolon off the one and not the other, i can't see any difference at all. unless you've got some kind of syntax error (like a left-off semicolon, maybe?) you really shouldn't be getting compilation errors off either one of these that the other one wouldn't get. could you post the actual error text you're getting, please?
this error is bcos when u use a=a+4(comsidering a is a byte) the compiler converts a into an int( a widening conversion) and adds it with 4 and the resulting int is passed into a which is of type byte(a narrowing conversion) so u get a compiler error saying it cannot convert int to byte or inappropriate types etc where as if u use a+=4 the compiler doesnt give an error cos += operator works in a different way compared with +
You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!
Joined: Jan 14, 2005
whoops. thanks, mr. Friedman, for correcting me. i think i've been too spoiled with dynamically typed languages; i'll have to try harder to adjust my thinking for static typing.