Why is that I can assign a integer value to a double variableBecause it's a widening conversion that doesn't lose any information. See JLS #5.1.2.
but cannot assign a double value to an integer variable?Because it's a narrowing conversion that might lose information. You can do it as long as you write an explicit typecast. See JLS #5.1.3.
Aren't double and integer compatible types?No, see above.
Isn't there a concept of truncation in java?Yes, see above.
964980 wrote:The second link IS about truncation, only different terminology is used (narrowing). Don't discard based on keywords, read it and understand it and only THEN draw conclusions.
Thanks for the replies.
The two links don't talk about truncation. Can you please share some link regarding truncation? I searched in google but could not find any good site which talks about truncation.