Java Puzzlers笔记--puzzle 9: Tweedledum +=的问题

    技术2022-05-11  61

     Provide declarations for the variables x and i such that this is a legal statement:  x += i;but this is not: x = x + i;

    Solution: short x = 0; int i = 123456; x += i; // Contains a hidden cast! x = x + i; //Won't compile - "possible loss of precision"

    TID: compound assignment expressions automatically cast the result of the computation the

    perform to the type of the variable on their left-hand side. do not use compound assignment operators on variables of type byte, short, or char.


    最新回复(0)