This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
The reason you can assign it is that it counts as a widening primitive conversion, which the language permits implicitly. Even though there is likely to be loss of precision. I think widening may refer to the range rather than the precision; every valid long value can be mapped to a float value, even though several longs can map to the same float.
If you try it the other way round, there are float values which are completely outwith the range of longs, so that counts as a narrowing conversion and you need an explicit cast. Anyway, the JLS link I gave you is quite clear.
Steve Fahlbusch wrote:when using java, there is no specification as to how many bits (bytes) a type utilizes.
JLS 4.2 Primitive Types and Values:
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units
JLS 4.2.3 Floating Point Types, Formats, and Values:
The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).
Although the physical size in a given JVM implementation may be larger than the sizes mentioned above, the various types must occupy at least as many bits or bytes as indicated. More to the point, though, the types must behave as though they were of those sizes, and that directly drives the OP's question.
McDonalds used to have signs in front of many of their stores, telling how may people they has served. I remember when they use to say something like "350 million served". The number kept going up, and eventually it was changed to "2 billion served", or whatever.
They went from a 3-digit number (350) to a 1-digit number (2) - because there is another part that tells you the magnitude. Some accuracy has been lost, but you could change the 350 into a 1-digit number by saying ".3 billion". or if you'd prefer... "3 onehundredmillion".
That's more or less how floating point numbers work in just about any computer language. You have the "value" part, and the "magnitude" part. When you convert from on to the other, some accuracy is lost, but it is often possible.
Further, not every number can be stored. Just like on the McDonald's sign, they could store 350 million, then 351 million...missing 999,999 number in between. When they converted to billions, they could go from 2 billion to 3 billion, missing quite a few numbers in between.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors