• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float and Double Query numeric promotion codes  RSS feed

 
Parag J Patankar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am new to Java, try to understand following if I declare following to do x + y

double x = 39.21;
float y = 2.1;

it gives me compile error as float number not define correctly. 

But if I declare following to do x * y / z

short x = 14;
float y = 13;
double z = 30;

it does not give me compile error. Why though I have not declared floating number correctly.

Pl help me to understand numeric promotions for float and double.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parag J Patankar wrote:
double x = 39.21;
float y = 2.1;

it gives me compile error as float number not define correctly. 


The "2.1" floating point literal is a double literal value. And you can't do an implicit cast from double to float.

If you want a "2.1" as a float literal value, then you should add an "f" ... meaning "2.1f".

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You're assigning a double literal (2.1) to a float variable. That is not allowed. You can only assign integral numbers to float, or floats. A float literal would look like this: 2.1F
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parag J Patankar wrote:
short x = 14;
float y = 13;
double z = 30;

it does not give me compile error. Why though I have not declared floating number correctly.


The "13" is an int literal value. And the assignment is allowed because you can do an implicit cast from int to float.


Side note (since some will point this out): The "14" is an int literal value. And since Java doesn't allow an implicit cast from int to short, why is the short assignment allowed? Basically, this is allowed due to a compile time constant rule. Since, "14" is a compile time constant, the Java compiler can figure out that no precision loss will happen during the assignment, and hence, it compiles. And interestingly, this compile time constant rule does not apply to floating point types.

Henry
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

This sort of thing will compile, but I think you shouldn't use floats at all. They lack the precision of a double.Have you tried promoting a Float (capital F) to a Double (capital D) (objects not primitives)? You cannot mix widening conversion and boxing like this, so you shouldn't be able to get this sort of thing to compile at all:-I challenge you to add casts and get the second bit of code to compile.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!