Assigning a wider value to a smaller type causes compile-time error,and casting is necessary for narrowing.But this code doesn't results in compile-time error.I assign a long(8bytes) value to a float(4bytes).
If doesn't results in compile-time error,does compiler provides implicit narrowing.Please explain.
Hi, even though it may seem that conversion from long(64bits) to float(32bits) is a narrowing conversion, it truly isn't. The point is floats are represented using scientific notation which handles larger range of values. To get a good explanation, please, refer to the following article courtesy of Corey: http://radio.javaranch.com/corey/2004/06/01/1086120368000.html
Regards, Paul. [ May 19, 2008: Message edited by: Paul Prusko ]
Why compiler did invoke the method with floating parameter,whereas the invocation is on long argument.How is it possible for a compiler narrowing long parameter to match floating argument.Can anyone explain this.
You see, the compiler is not narrowing long primitive to float. As I wrote in my previous post a long can fit in the float parameter type and this is what the compiler is doing - it matches long type parameter to tha method definition which takes the smallest type but wider than long, in case the exact type is not found - which is what you have in your code. This is all about grasping the idea of how numbers are represented and which one can fit into another. Please, have a look at the article which I suggested you before.