aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Dan Watch "Doubt on Dan New topic
Author

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: http://aspose.com/file-tools
 
subject: Doubt on Dan's Question
 
Similar Threads
Right shift using array
mock exam question
Adding bytes and getting int
int as expression value
how can I assign -1 as Hex Literal to byte type var?