Java中的基本类型等级图:
位于箭头右边的等级高于箭头左边的等级。
short s1 = 5;
s1 = s1 - 3; //编译错误 @1
@1中的表达式 中s1 将自动提升到int类型,则右边的表达式类型为int, 将 int 类型赋给 short类型的变量时将发生错误。
System.out.println("hello!" + 'a' + 7); //输出 hello!a7
System.out.println('a' + 7 + "hello!"); //输出 104hello!
@1中 s1 = s1 - 3;将引起编译错误,但改为 s1 -= 3;则正确,
s1 = s1 - 3;
s1 -= 3;
两者并不一样, s1 -= 3 等价于 s1 = (s1的类型) (s1 - 3); 可见复合赋值运算 包含一个 隐式的类型转换
同理 ( +=、 -=、 *=、 /=、%= .....)也类似
转义字符的陷阱:
1.慎用unicod转义形式
System.out.println("ac\u000a"); //编译错误
\u000a相当于一个 换行符,转换过来就是:
System.out.println("ac
"); //编译错误
行注释中使用unicode:
//\u000a表示
上面的注释报错,因为编译后的形式是:
//
表示
char c = 0x000a;
System.out.println(c); //同样报错