+ |
正号 |
+3 |
3 |
+ |
加 |
2+3 |
5 |
+ |
连接字符串 |
“中”+“国” |
“中国” |
- |
负号 |
int a=3;-a |
-3 |
- |
减 |
3-1 |
2 |
* |
乘 |
2*3 |
6 |
/ |
除 |
5/2 |
2 |
% |
取模 |
5%2 |
1 |
++ |
自增 |
int a=1;a++/++a |
2 |
-- |
自减 |
int b=3;a--/--a |
2 |
算数运算符++、--的使用
int a = 3;
int b = 3;
a++;
b--;
System.out.println(a);
System.out.println(b);
上面代码的输出结果a值为4,b值为2;
a的原有值发生了改变,在原有值的基础上自增1;b的原有值也发生了改变,在原有值的基础上自减1;
int a = 3;
int b;
b = ++a + 10;
System.out.println(a);
System.out.println(b);
上面代码的输出结果a值为4,b值为14;
l ++,--运算符前置时,先将变量a的值自增1或者自减1,然后使用更新后的新值参与运算操作。
赋值运算符
赋值运算符
* +=, -=, *=, /=, %= :
* 上面的运算符作用:将等号左右两边计算,会将结果自动强转成等号左边的数据类型,再赋值给等号左边的
* 注意:赋值运算符左边必须是变量
publicclass OperatorDemo2 {
publicstaticvoid main(String[] args) {
byte x = 10;
x += 20;// 相当于 x = (byte)(x+20);
System.out.println(x);
}
}