-3

I know its kind of stupid question. Wanted to know how the code works.

Code 1:

float a =123L;

Here, even though the number 123L is of long type (8 bytes) and the float a(4bytes) i don't get the compile time error as "Possible lossy conversion"

Code 2:

long c=123.0D;

Here even though the double and long type are of 8 bytes,I still get the "Possible lossy conversion" compile time error.

I was thinking the values which are getting stored in the variable should be the both Type compatible.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • 2
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Blackbelt May 10 '16 at 14:17
  • 2
    Welcome to stack overflow. Please understand that you should do some prior research before writing up a new question. And especially ... any kind of "newbie" questions ... you can be pretty sure, that it was asked here countless times before. – GhostCat May 10 '16 at 14:20

1 Answers1

0

Floating point data types represent a very close approximation, depending on the number itself and the system, precision etc. So if you convert eg 340L to floating point it might for example be represented as 339.999 instead of 340.

Integers and floating points are represented completely different from each other which is why you are getting warned about a possible loss of precision since integers represent an exact number unlike floating points which contains a mantissa.

vlind
  • 629
  • 6
  • 16