
3个回答
展开全部
你所问的这个问题叫“自动类型提升”
在java中,对char, byte和short类型的字符串进行数学算法操作,比如+,-,>>位移操作,都会引发
自动类型提升,结果将是int类型,a+b的结果是int, 要将int的值赋给byte就必须进行类型强制转换。
以下这句,是从think in java 3rd中摘出来的,希望对你有帮助。
In char, byte, and short, you can see the effect of promotion with the arithmetic operators. Each arithmetic operation on any of those types produces an int result, which must be explicitly cast back to the original type (a narrowing conversion that might lose information) to assign back to that type.
最后 关于类型的自动提升,java定义若干适用于表达式的类型提升规则:
第一,所有的byte型、short型和char型的值将被提升到int型;
第二,如果一个操作数是long型,计算结果就是long型;
第三,如果一个操作数是float型,计算结果就是float型;
第四,如果一个操作数是double型,计算结果就是double型。
在java中,对char, byte和short类型的字符串进行数学算法操作,比如+,-,>>位移操作,都会引发
自动类型提升,结果将是int类型,a+b的结果是int, 要将int的值赋给byte就必须进行类型强制转换。
以下这句,是从think in java 3rd中摘出来的,希望对你有帮助。
In char, byte, and short, you can see the effect of promotion with the arithmetic operators. Each arithmetic operation on any of those types produces an int result, which must be explicitly cast back to the original type (a narrowing conversion that might lose information) to assign back to that type.
最后 关于类型的自动提升,java定义若干适用于表达式的类型提升规则:
第一,所有的byte型、short型和char型的值将被提升到int型;
第二,如果一个操作数是long型,计算结果就是long型;
第三,如果一个操作数是float型,计算结果就是float型;
第四,如果一个操作数是double型,计算结果就是double型。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询