public class DemoString{
public static void main(String[] args){
System.out.println(5 + 5 + " 5 + 5 = " + 5 +5 );
}
}
out: 105 + 5 55
自左向右运算,+ 号有字符串参与的话就是连接的作用 ,因为他没法直接运算. 任何类型和字符串相加都会变成字符串类型 字符串就相当于生化危机中的僵尸,碰到谁就把谁感染成僵尸
public class Demo01{
public static void main(String[] args){
short s = 1;
s = s + 1;
System.out.println(s);
}
}
out:
Demo01.java:4: 错误: 不兼容的类型: 从int转换到short可能会有损失
s = s + 1;
^
1 个错误
char/ short/byte类型的数,参与运算时,自动转换为int类型
public class Demo01{
public static void main(String[] args){
short s = 1;
s++;
System.out.println(s);
}
}
out: 2
public class Demo01{
public static void main(String[] args){
short s = 1;
s+=1;
System.out.println(s);
}
}
out: 2
s += 1; ===> s = short(s+1)
对右侧的变量表达式,整体进行强制类型转换