/**
* char byte short类型执行算术运算符和按位运算符,在运算之前,这些类型会先上升为int类型,在参与运算.
* 但如果只存在自增和自减,则不会.
*
*
*/
public class charToInt {
public static void main(String[] args) {
//以char为例子,byte和short也一样
char c = ‘a’;
char b = (char) (c+5);//int,这里的c会上升为int类型,所以需要强转为char类型
System.out.println(b);
System.out.println(c+5);
char d = ‘a’+5;//还是char类型,因为这里表示的是在’a’后面的第11个字符.如果先声明’a’是char类型,就同上了.
System.out.println(d);
System.out.println(++c);//和char d = ‘a’+5;的原理是一样的.
}
}
01-20
05-01
05-01
05-01
05-01
05-01
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交