Java基本类型转换(Primitive Data Type Casting) int/byte & char

From <Java Puzzlers>


byte->char: a byte is converted to an int and the int to char. byte->int->char

  符号位扩展

  char c = (char) b;  // Sign extension is performed

  无符号扩展

  char c = (char) (b & 0xff);



char->int

  符号位扩展

  int i = (short) c;  // Cast causes sign extension

  无符号扩展

      写法一:

   int i = c & 0xffff

     写法二:

   int i = c;  // Sign extension is not performed

你可能感兴趣的:(java)