File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes does += (in general - all shorthand operators) imply an implicit cast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "does += (in general - all shorthand operators) imply an implicit cast?" Watch "does += (in general - all shorthand operators) imply an implicit cast?" New topic
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: http://aspose.com/file-tools
 
subject: does += (in general - all shorthand operators) imply an implicit cast?