希望大家不吝赐教~~1、我知道short+int时要把short变成int才行,但是呢,下面这个的结果为何是int?byte b = 1;
char c = 'A';// b+c 这个是int型我想是不是因为char是无符号的?为了绕开符号位只能再往上提高一级?2、这个也转型?
byte b = 1;// b = b+b; b+b是int型。。3、byte转char时为何要先将byte转成int?这几个问题实在搞不定了,跪地360度求解
char c = 'A';// b+c 这个是int型我想是不是因为char是无符号的?为了绕开符号位只能再往上提高一级?2、这个也转型?
byte b = 1;// b = b+b; b+b是int型。。3、byte转char时为何要先将byte转成int?这几个问题实在搞不定了,跪地360度求解
如果你用字符串就不同
如果你用字符串就不同
如果你用字符串就不同
你记住就行了
//所得结果的数据类型与精度较高(或位数更长)的那种数据类型一致public class TestOperation {
public static void main(String[] args) {
byte b = 1;
char c = 1;
short s = 1;
int i = 1;
float f = 1;
double d = 1;
long l = 1;
//byte char short 和 他们三者之一相加后 系统会将结果转为int型,如果要还原原来类型就得向下强转了
int tempInt = b+b;//->int
tempInt = b+c;//->int
tempInt = b+s;//->int
tempInt = b+i;//->int
long tempLong = b+l;//->long
float tempFloat = b+f;//->float
double tempDouble = b+d;//->double
tempInt = c+c;// -> int
tempInt = c+s;// -> int
tempInt = c+i;// -> int
tempLong = c+l;// -> long
tempFloat = c+f;// -> float
tempDouble = c+d;// -> double
tempInt = s+s;// -> int
tempInt = s+i;// -> int
tempLong = s+l;// -> long
tempFloat = s+f;// -> float
tempDouble = s+d;// -> double
tempInt = i+i;// -> int
tempLong = i+l;// -> long
tempFloat = i+f;// -> float
tempDouble = i+d;// -> double
tempLong = l+l;// -> long
tempFloat = l+f;// -> float
tempDouble = l+d;// -> double
tempFloat = f+f;// -> float
tempDouble = f+d;// -> double
tempDouble = d+d;// -> double
}}
比如这段代码: byte b = 1;
char c = 1;
short s = 1;
int i = 1;存储的指令都是 istore_1,istore_2,istore_3...
istore就是将栈顶int类型数值存入指定的本地变量。所以都以int来对待;这段代码: tempInt = b+b;//->int
tempInt = b+c;//->int
tempInt = b+s;//->int
tempInt = b+i;//->int相加的指令,不管是byte+byte还是byte+short等,都是iadd
iadd就是讲栈顶两个int类型的数相加,然后压入栈。结果还是int。所以,这就是“规定”的根源了