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 narrowing the reference 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 "narrowing the reference" Watch "narrowing the reference" New topic

narrowing the reference

Sricharan Modali

Joined: Mar 30, 2004
Posts: 19
The out of this program is given to be float, float where as i was thinking it sud be float, double as int and float are 32 bits and long and double are 64!
Any possible explanations are helpful.
class GFC215 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2;
System.out.print(m(a1)+","+ m(b1));

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Keep in mind that casting from a long to a float is still considered a widening conversion. Seems strange as a long is 64 bits and a float is only 32, doesn't it? But it's true.
Here's the reasoning...
A widening conversion is a conversion in which you can convert a value of one type to that of another type safely (without losing data). Converting an int to a byte is obviously not safe as you might lose a lot of data. So, why then, is a conversion from a long to a float "safe?"
Well, the answer is in the ranges. What's the maximum value of a long?
2^63 - 1
What's the maximum value of a float?
Which one is greater? Obviously, the maximum value of a float is higher. Therefore, any value that falls within the range of a long also falls within the range of a float. That makes a conversion from a long to a float "safe."
How is that possible if a long is 64 bits and a float is 32 bits. Well, the answer to that question lies in the way we interpret those bits. Bits are bits - the computer sees them as 1's and 0's and nothing more. We, however, interpret them in some way. When we look at a long, we see a 63 bit representation of a value and a sign bit. When we look at a float, we see much more - there's a sign bit and there's a number in there, but there's also an exponent. Floats and doubles are the Java version of scientific notation, so to speak.
Certainly, you can write out 1,000,000,000, but you can also write 1x10^9 and save yourself a few characters. It's the same way with longs and floats.
However, there's one caveat here. When you boil it all down, 64 bits is still more than 32 bits and there are more combinations possible with 64 bits than there are in 32 bits. Therefore, even though a given long value falls within the "range" of a float, there is always a chance that the value is question can't be represented exactly by a float. What you end up with is an awfully close approximation.
So, I've probably gone into greater detail than you needed, but keep in mind that the order of conversions from int to double (from narrowest to widest) goes something like this:
int -> long -> float -> double
I hope that helps,

SCJP Tipline, etc.
Sricharan Modali

Joined: Mar 30, 2004
Posts: 19
Thanks Corey That was really helpful.
I agree. Here's the link:
subject: narrowing the reference
It's not a secret anymore!