• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment operator

 
tnemani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tmemani

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 +

hope that explains ur doubt
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, actually, he should be seeing an error.

The difference is that the expression "a+4" is of type integer, and therefore the compiler won't let you directly assign it to the byte variable "a" without a cast; i.e., "a = (byte) (a + 4)" is OK.

With the += operator, though, that cast is built-in, and so the assignment compiles without a problem. See Section 15.26.2 of the Java language spec:



A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.


See that cast "(T)" in there? That would be "byte" in our example.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compound assignment operator includes an implicit cast to type of the LHS. That is why you are not getting a compile time error.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tnemani --

Welcome to JavaRanch!

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!
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic