File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Dan's Question

 
Vidyavathi saravanan
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The answer is Compile-time error. Anybody knows the real reason?
 
Chris Allen
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic