• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Type Conversion

 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Paul Prusko
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Paul.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul for suggesting the link to Corey's tipline.Its really great to know the intricases of floating point representations by IEEE.Its really worth reading.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it...went through this article http://radio.javaranch.com/corey/2004/06/01/1086120368000.html

Thanks for your help
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic