File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Casting question

sai prashanth

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..

Henry Wong

Joined: Sep 28, 2004
Posts: 20521

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Casting question
jQuery in Action, 3rd edition