aspose file tools*
The moose likes Java in General and the fly likes Casting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Casting question" Watch "Casting question" New topic
Author

Casting question

sai prashanth
Greenhorn

Joined: May 04, 2010
Posts: 18
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
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting question