对于
char x='a';可以写成
char x=97;为什么下列写法却是错误的\为什么不能这样赋值?
char x='a'+2;'b'+2 的结果是int型的,书上这么说的.

解决方案 »

  1.   

    Binary operators convert the smaller(less precise) operand to the type of the larger(more precise) operand. 
    All operators will convert things smaller than ints to ints or larger. This includes char 's! 
    1.byte,char,short默认转成int
    2.byte->short->int->long->float->double
    char^
      

  2.   

    你的意思是说,INT型精度较大,当把INT型的赋给CHAR时,精度损失,所以前面加上(char)
    是这样的吗?
      

  3.   

    'a'+2计算完系统进行了类型提升成int,所以要来个强制转换(char)
      

  4.   

    还是不明白啊,既然'a'+2已经是INT型的了,而且类似char x=97;又是正确的.为何还要来个强制转换?
      

  5.   

    晕了
      
     一个就是给CHAR类型赋值,一个是将一个INT类型的赋给char类型的
      

  6.   

    如 tomuno(特别行动组) 所说byte->short->int->long->float->double
    char 是属于byte型的,97是在byte的范围内,所以char a=97
    97是在byte的范围内,系统就认为这个97是byte,就没什么错了,但是你假如int i=97;
    再char a = i;的话,系统肯定会报错的,原因如上的转换方向所示还有假如你的 char a =xxx,这个xxx超过byte的范围的话,系统也会报错的
    如char a = 32767,肯定报错,
    所以楼主的那个 char a = 'a'+2;属于第二种情况