• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Question about implicit promotion

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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: 23873
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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

 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic