在java中的数值计算时要进行类型自动提升,例如将byte,short,char提升为int类型
如下的代码:
package Web; /** * java在进行计算时,会对byte, * short,char类型进行自动类型 * 提升,提升为int类型的 */ public class Main{ public static void main(String[] args){ byte b1=1,b2=2,b3,b6; final byte b4= 4,b5 = 5; b6=b4+b5; b3=(b1+b2); //编译通不过 System.out.println(b3+b6); } }
在上面的代码中进行计算时b1+b2分别是byte类型,但是进行计算时java会自动提升为int类型,所以当用一个byte的b3去接收时编译会认为类型不匹配。