File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting hierarchy" Watch "Casting hierarchy" New topic

Casting hierarchy

Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
I'm not sure if my wording is exactly correct, so correct me if it's wrong....
It seems casting is necessary when initializing anything below a selected type on the list:

double d = 9;
float f = (float)d; //Cast necessary
1) Example:

Why is casting necessary between float and long even though float is 32 bits and long is 64 bits? Long is bigger, there should be plenty of room.

I believe that short can hold an negative value, and char can't. So, a char can hold more than a short? and a cast is necessary. Why is a cast necessary both ways if one is bigger than the other?

Why is casting necessary both ways if char is 16 bits, and byte is 8 bits?
4)O'yea, by the way, thanks for you help!!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In JLS 5 all of this is explained.
Any assignment between char and short/byte is regarded as a narrowing conversion, and thus it needs a cast... Except the cases explained in JLS 5.2

SCJP2. Please Indent your code using UBB Code
Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
What is JLS? Java L... S...
Patrick Hoey

Joined: Oct 11, 2001
Posts: 5
Java Language Specification.
Get it here:
You can download it in html, or pdf format (about 4.5 megs).
I agree. Here's the link:
subject: Casting hierarchy
It's not a secret anymore!