Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will we loose precision by implicit conversion

There is a question in Java past exam paper that brothers me:

With implicit conversion of primitive data types, you can lose precision and get incorrect results.

A True, B False

The key to the answer is A: True

I think it will neither lose precision nor get incorrect results. I know the explicit conversion can lose precision and get incorrect results but not implicit one.

For example:

int i = 9;

short s = 3;

i = s; // implicit conversion, neither loose 
      //precision nor incorrect results

s = i; // compile error, do we call this implicit conversion? 
       //if yes, then the answer to question 3 is True, 
       //but I don't think this is an implicit conversion, 
       //so I think answer is false.   

As states on the notes:

Implicit type conversion: The programmer does not make any attempt to convert the type, rather the type is automatically converted by the system under certain circumstances.

Could anyone please advise?

Many thanks.

like image 890
Huibin Zhang Avatar asked Jun 12 '26 01:06

Huibin Zhang


1 Answers

Answer = A

    float f = Long.MAX_VALUE;
    System.out.println(Long.MAX_VALUE);
    System.out.printf("%.0f", f);

output

9223372036854775807
9223372036854776000
like image 96
Evgeniy Dorofeev Avatar answered Jun 14 '26 14:06

Evgeniy Dorofeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!