This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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