Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question about implicit promotion

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having troubling trying to figure out implicit promotions - since int and float are 32-bit, I assumed it'd be okay to assign one to another. It works for assigning an int to a float e.g.

int y=5;
float f=y;

But not the other way around, ie when I try to assign a float to an int I get a compiler error -

float ff=5.0f;
int x=ff;

Why should this be so since both, float and int are 32-bit??

Thanks.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

k reeta wrote:
Why should this be so since both, float and int are 32-bit??



It's not based on the number of bits. It's based on the range. Float has a much larger range than an int, so implicit casting from int to float is okay, but not in the other direction.

Henry
 
Ranch Hand
Posts: 50
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

k reeta wrote:Hi,

I am having troubling trying to figure out implicit promotions - since int and float are 32-bit, I assumed it'd be okay to assign one to another. It works for assigning an int to a float e.g.

int y=5;
float f=y;

But not the other way around, means when I try to assign a float to an int I get a compiler error -

float ff=5.0f;
int x=ff;

Why should this be so since both, float and int are 32-bit??

Thanks.


Here is the order in ascending:
BYTE <SHORT <INT <LONG<FLOAT<DOUBLE.....

Smaller value casted implicitly....(check your first example: float is greater so assigning int value to float -OK)
But not the other way around.
If you want to assign float value to int do explicit cast.
float ff=5.0f;
int x = (int)ff; int is lower in the order and you are assigning higher element to lower one
 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for the responses, I think I've got it now!
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type wise
BYTE <SHORT <INT<LONG<FLOAT<DOUBLE.....
is correct

size wise
BYTE<SHORT<INT<FLOAT<LONG<DOUBLE.....
is correct


Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value.
Refer JLS 5.1.2

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic