In java why we can't assign int to char directly??? but vice-versa is true?

前端 未结 4 956
夕颜
夕颜 2020-12-22 02:18
public class CharAndIntTest {

    public static void main(String[] args) {
        char i=\'1\';
        int ii=65;
        i=ii;
    }

}
4条回答
  •  無奈伤痛
    2020-12-22 02:42

    chars are 16-bit unsigned numbers, so they got a maximum of 65,635.
    ints are 32-bit signed numbers,s o they got a maximum of 2,147,483,647.

    So something is bound to go wrong if you sign an int to a char, for all values >= 65,635.

提交回复
热议问题