• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cast compiler error

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following example results in a compiler exception and needs a cast to execute.



For primitives, I know that the result of arithmetic operations with byte and short result in int and need casting to avoid compiler errors. Number literals are by default int and hence in this example a cast is needed. Am I right?
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. And please quote your sources from now on
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
giving a method expecting a short an int will cause compiler error because int which is a 32 bit 2's complement integer can cause overflow if it's stored in short which is only 16bit, however the reverse is ok, you can store short integer in int since int is an extension of short
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic