Actually, the paragraph in the book makes a general point that float and double also lose information when you assign an int or a long to them respectively. And that is correct. Detailed understanding of why this happens is not required for the exam but you may go through
this discussion to learn more.
Now, coming to your code. Two things:
1. The text in the book does not claim that information is lost in every assignment. Indeed, once you read about how floating point numbers are stored (not required for the exam), you will realize that information is not lost in every possible assignment. For example, assigning a long containing 0 (zero) to a double will not lose information.
2. Just because == operator returns true does not mean no information is lost. If you print out the value of value and doubleValue, you will see the following output:
Code: Select all
9223372036854775807
9.223372036854776E18
Do you see the difference? They are not exactly the same. Yet, == operator prints true because of numeric promotion (long value gets promoted to double).
HTH,
Paul.