可以参考一下 Java Language Specification, 3rd ed.(JLS 3)第 5.6.2 节的 Binary Numberic Promotions,即“双目数值类型提升” http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2# If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then: # If either operand is of type double, the other is converted to double. # Otherwise, if either operand is of type float, the other is converted to float. # Otherwise, if either operand is of type long, the other is converted to long. # Otherwise, both operands are converted to type int. # 如果其中有一个操作数是引用类型的话,将会执行拆箱转换,然后: # 如果其中任意一个操作数的类型为 double,那么另外一个会被转换为 double 类型 # 否则,如果其中任意一个操作数的类型为 float,那么另外一个会被转换为 float 类型 # 否则,如果其中任意一个操作数的类型为 long,那么另外一个会被转换为 long 类型 # 否则,两个操作数都会被转换为 int 类型根据 JLS 3 中关于双目数值类型提升的规则,可以看出,任意个数值类型的数与 double, float, long, int 参与二目运算时,会被转为 doube, float, long, int 之后再进行运算。根据 JLS 3 规定的规则,对于类型提升就很明朗了:char + char 会被转为 (int)char + (int)char char + int 会被转为 (int)char + int int + long 会被转为 (long)int + long char + byte 会被转为 (int)char + (int)byte char * short 会被转为 (int)char * (int)shortCharacter + byte 拆箱为 char + byte,最终再转为 (int)char + (int)byte
这只是二目运算的数值类型自动转换,如果碰到三目运算符,那情况会变得更复杂!详见 JSL 3 中 15.25 节“条件运算符” http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25____●_如果第二和第三个操作数在可以转换为数值类型时,会有以下几种情况: _______◆_操作数其中一个是 byte 或 Byte 类型,而另一个是 short 或 Shoft 类型,那么 _________ 这个表达式就是 shoft 类型 _______◆_操作数中的一个是类型 T (T 可以是 byte、short 或者是 char 类型),而另一个 _________ 是 int 类型的常数,其可以用 T 类型来表示时,那么这个表达式就是 T 类型 _______◆_操作数中的一个是 Byte 类型,而另一个是 int 类型的常数,其可以用 byte 类型来 _________ 表示,那么这个表达式就是 byte 类型 _______◆_操作数中的一个是 Short 类型,而另一个是 int 类型的常数,其可以用 short 类型 _________ 来表示,那么这个表达式就是 short 类型 _______◆_操作数中的一个是 Character 类型,而另一个是 int 类型的常数,其可以用 char 类 _________ 型来表示,那么这个表达式就是 char 类型 _______◆_否则,双目数值提升(binary numeric promotion)会被用于操作数的类型中,条件表 _________ 达式的类型是第二个和第三个操作数提升后的类型。注意:双目数值提升时进行拆箱转换和 _________ 值集转换(value set conversion)
double > float > long > int > byte/short/char Integer,Double,Float,Long...拆箱。
是不是括符不配对?
int + char = char型吗???
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2# If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
# If either operand is of type double, the other is converted to double.
# Otherwise, if either operand is of type float, the other is converted to float.
# Otherwise, if either operand is of type long, the other is converted to long.
# Otherwise, both operands are converted to type int. # 如果其中有一个操作数是引用类型的话,将会执行拆箱转换,然后:
# 如果其中任意一个操作数的类型为 double,那么另外一个会被转换为 double 类型
# 否则,如果其中任意一个操作数的类型为 float,那么另外一个会被转换为 float 类型
# 否则,如果其中任意一个操作数的类型为 long,那么另外一个会被转换为 long 类型
# 否则,两个操作数都会被转换为 int 类型根据 JLS 3 中关于双目数值类型提升的规则,可以看出,任意个数值类型的数与 double, float, long, int 参与二目运算时,会被转为 doube, float, long, int 之后再进行运算。根据 JLS 3 规定的规则,对于类型提升就很明朗了:char + char 会被转为 (int)char + (int)char
char + int 会被转为 (int)char + int
int + long 会被转为 (long)int + long
char + byte 会被转为 (int)char + (int)byte
char * short 会被转为 (int)char * (int)shortCharacter + byte 拆箱为 char + byte,最终再转为 (int)char + (int)byte
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25____●_如果第二和第三个操作数在可以转换为数值类型时,会有以下几种情况:
_______◆_操作数其中一个是 byte 或 Byte 类型,而另一个是 short 或 Shoft 类型,那么
_________ 这个表达式就是 shoft 类型
_______◆_操作数中的一个是类型 T (T 可以是 byte、short 或者是 char 类型),而另一个
_________ 是 int 类型的常数,其可以用 T 类型来表示时,那么这个表达式就是 T 类型
_______◆_操作数中的一个是 Byte 类型,而另一个是 int 类型的常数,其可以用 byte 类型来
_________ 表示,那么这个表达式就是 byte 类型
_______◆_操作数中的一个是 Short 类型,而另一个是 int 类型的常数,其可以用 short 类型
_________ 来表示,那么这个表达式就是 short 类型
_______◆_操作数中的一个是 Character 类型,而另一个是 int 类型的常数,其可以用 char 类
_________ 型来表示,那么这个表达式就是 char 类型
_______◆_否则,双目数值提升(binary numeric promotion)会被用于操作数的类型中,条件表
_________ 达式的类型是第二个和第三个操作数提升后的类型。注意:双目数值提升时进行拆箱转换和
_________ 值集转换(value set conversion)
Integer,Double,Float,Long...拆箱。