File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Dan's Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Dan Watch "Doubt on Dan New topic

Doubt on Dan's Question

Vidyavathi saravanan
Ranch Hand

Joined: Sep 24, 2004
Posts: 34

The answer is Compile-time error. Anybody knows the real reason?
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Originally posted by Vidyavathi saravanan:

The answer is Compile-time error. Anybody knows the real reason?

The ++ operator (and -- operator) provide an automatic cast back to the correct primitive type automatically for you. The (byte)a+b statement is really saying cast a to byte and then add b. Since all integer primitive classes (byte, short, char) are automatically converted to an int for the + operation, the result is an integer value trying to be assigned to a byte value without an explicit cast (narrowing conversion must occur). The (byte)a will be promoted to an int to be the same as the value b to perform the arithmetic operation. Thus, there is a compiler error as you are trying to assign an int to a byte.
I agree. Here's the link:
subject: Doubt on Dan's Question
It's not a secret anymore!