| Author |
does += (in general - all shorthand operators) imply an implicit cast?
|
Aakash Goel
Ranch Hand
Joined: May 26, 2008
Posts: 198
|
|
What is wrong with...
char c = 'a';
c=c+4;
considering that
char c='a';
c+=4;
works fine!!
Thanks in advance!
|
SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
|
 |
Tom Kowalski
Ranch Hand
Joined: Feb 17, 2009
Posts: 72
|
|
It`s because, those expressions are similar but not exactly this same. When you use ‘+=’ java will cast the expression automatically so when you write:
The compiler will see something like this:
P.S I suggest you K&B book, you will find there many answers which will appear for sure, while you are learning for SCJP ^^
|
SCJP, SCWCD, OCUP
|
 |
Paolo Dina
Ranch Hand
Joined: Aug 15, 2008
Posts: 63
|
|
Just to reply to the question in the subject, YES. Quote from JLS.
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.
|
SCJP 5
|
 |
Aakash Goel
Ranch Hand
Joined: May 26, 2008
Posts: 198
|
|
|
just wanted to confirm that implicit cast is implied. thanks everyone
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: does += (in general - all shorthand operators) imply an implicit cast?
|
|
|