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

Casting question

 
sai prashanth
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I have this silly doubt about casting and any clarification is appreciated.

I understand the byte, short , integer, long are 8,16,32,64 bits respectively.

so when I do this:



..the compiler does not complain even though '9' an integer literal of 32 bits is assigned to a 16 bit, is that right?

On the other hand


I dont understand why..

Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned in the other topic, the first rule that you are referring to, is related to assignment of compile time constants. During assignments, if the compiler is able to figure out, at compile time obviously, that the value will fit, then no cast is needed.

And there is no such related rule for method calls.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic