0

My textbook says this:

Converting from a larger type to a smaller type is called a down cast, also known as range narrowing.

Byte <- short <- int <- float <- long <- double

Why is long higher than float on the downcasting/narrowing list?

Isn't the range of values represented by a float much wider than the range represented by a long?

E.g., when casting types (assuming f represents a float and l represents a long), you can do do f = l, but this requires an explicit cast: l = (long) f. Doesn't this show that a float should be higher than a long on the downcasting list?

Community
  • 1
  • 1

1 Answers1

0

A long and a double are both 64 bits whereas a float is only a 32 bit data type. Thus it takes a downcast to turn the long's 64 bit number into a 32 bit.

When programming, float has a wider range of number, but the long uses its 64 bits to create a more precise number. This is the reason you use float when you need to save space on a less precise number, but for something where precision is important (i.e., currency, scientific calculations, etc) a long is what you should be using.

If you're curious you can check out the Java Doc's here

user207421
  • 305,947
  • 44
  • 307
  • 483