希望大家不吝赐教~~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度求解

解决方案 »

  1.   

    因为char基本对应到ascii码所以计数时会用ascii
    如果你用字符串就不同
      

  2.   

    因为char基本对应到ascii码所以计数时会用ascii
    如果你用字符串就不同
      

  3.   

    因为char基本对应到ascii码所以计数时会用ascii
    如果你用字符串就不同
      

  4.   

    short,byte,char在运算时都会自动转换成int
    你记住就行了
      

  5.   

    short,byte,char在运算时都会自动转换成int,这是规定
      

  6.   

    只要是比int表示的范围小的(例如short,byte,char等),在运算时,都是自动转换成int
      

  7.   

      因为他们是在计算ACSSI码    不然你认为byte怎么可能和char运算呢
      

  8.   

    其实char类型是根据char运算的,只不过输出的时候计算机默认转换成十进制的int输出
      

  9.   

    //当参加二元运算的两个操作数的数据类型不同时,
    //所得结果的数据类型与精度较高(或位数更长)的那种数据类型一致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  
        }}
      

  10.   

    在JVM中 byte,short,char,int 类型的数值都是以整数形式存储的。
    比如这段代码:    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。所以,这就是“规定”的根源了
      

  11.   

    JVM指令关于两数相加只有 iadd(int相加), ladd(long相加), fadd(flaot相加), dadd(double相加)再没其他的了,加减乘除都一样
      

  12.   

    short,byte,char在运算时都会自动转换成int