java byte的一些运算注意点

public class Test001 {
	
	
	public static void main(String[] args) throws Exception {
		
		byte b1 = 3;
		byte b2 = 4;
		byte b3 ;
		
		//报错:Type mismatch: cannot convert from int to byte
		//jvm 对于整数变量默认为int类型
		//b3=b1+b2;
		
		b3 = 3+4;//在编译时就变成7,把7赋值给b,常量优化机制   class文件编译成:byte b3 = 7;
		
		//报错:Type mismatch: cannot convert from int to byte
		//b1=b1+1;
		
		b1+=1;//class文件编译成b1 = (byte)(b1 + 1); 进行了隐式转换
		
	}
}

 

你可能感兴趣的:(java)