Astha Sharma wrote:
Output-
true
false
While assigning i to f, some information will be lost as value of type float are not precise to nine significant digits.
If above statement is correct, line2 printing false is fine. But why does line1 prints true?
And why does the below code prints true even when Integer.MAX_VALUE>123456789?
Output-
true
This can be because of the margin of error in float and double values, which does not return the exact value.
Since float and int is of 32 bits, for the second case, all the bits are used for the maximum value of int, hence for
float f=i;
float doesn't have to - add any precision bit values/or deal with the precision error. So, the down casted value in j will be same as the value in i, hence
j==i is true.
While in the first case, since all the 32 bits are not utilized for the value in
int i, conversion to float(also 32bit), i.e
float f=i, will add some precision bits/or deal with the precision error, when value of "i" will be stored in f. So during down casting the casted value will not be same as the value in "i".
Though I'm not sure, this appears to be the correct reason. Waiting for more responses.