Why can I pass 1 as a short, but not the int variable i?

后端 未结 7 1873
谎友^
谎友^ 2020-12-07 16:18

Why does the first and second Write work but not the last? Is there a way I can allow all 3 of them and detect if it was 1, (int)1 or i passed in? And really why is one allo

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 16:46

    an int literal can be implicitly converted to short. Whereas:

    You cannot implicitly convert nonliteral numeric types of larger storage size to short

    So, the first two work because the implicit conversion of literals is allowed.

提交回复
热议问题