1,算术运算符
运算符 | 解释 |
+ | 正号 |
+ | 连接号 |
+ | 加号 |
- | 负号 |
- | 减号 |
* | 乘号 |
/ | 除号 |
% | 去余 |
++ | 自增 ++变量名:先加1再运算 变量名++:先运算再加1 |
-- | 与自增同理 |
public class L1 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=3;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(b%c);//2
System.out.println(++a);//11
System.out.println(--a);//10
}
}
2,关系(比较)运算符
运算符 | 解释 |
> | 大于 |
< | 小于 |
= | 赋值 |
>= | 大于等于 |
<= | 小于等于 |
== | 等于 |
!= | 不等于 |
注意:关系运算符的结果是布尔型(true/false)
public class L1 {
public static void main(String[] args) {
System.out.println(10<20);
System.out.println(10>20);
System.out.println(10<=20);
System.out.println(10>=20);
System.out.println(10==20);
System.out.println(10!=20);
}
}
3,逻辑运算符
运算符 | 理解 | 解释 |
& | 与 | 全真为真 |
| | 或 | 一真为真 |
! | 非 | 当为true时,结果为false。当为false时,结果为True |
^ | 异或 | 当两个都为true或false时,结果为false,其他相反 |
&& | 短路与 | 如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算。 |
|| | 短路非 | 当左边为真,右边不参与运算 |
public class l1 {
public static void main(String[] args) {
boolean bo=true;
boolean on=false;
System.out.println(bo&on);
System.out.println(bo|on);
System.out.println(!on);
System.out.println(bo^on);
System.out.println(bo&&on);
System.out.println(bo||on);
}
}
4,赋值运算符
注意:①相加时,默认它的数据类型,不同数据类型使用自动类型转换或使用强制类型转换原则
②:可以连续赋值。
运算符 | 解释 |
= | 赋值 |
+= | a+=b a=a+b |
-= | a-=b a=a-b |
*= | a*=b a=a*b |
/= | a/=b a=a/b |
%= | a%=b a=a%b |
public class l1 {
public static void main(String[] args) {
int a=10;
int b=11;
System.out.println(a=b);
System.out.println(a+=1);
System.out.println(a-=b);
System.out.println(a*=b);
System.out.println(a/=b);
System.out.println(a%=b);
}
}
5,字符串连接运算符
注意:ASCII的使用
public class l1 {
public static void main(String[] args) {
int a=10;
String b="嗡嗡嗡";
char c='a';
char d='b';
System.out.println("a的数值是"+a+b);//a的数值是10嗡嗡嗡
System.out.println(c+d);//195
}
}
6,条件运算符(三元运算符)
格式:
变量3=变量1>变量2?数值1(其他数值):数值2(其他数值);
public class l1 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=a>b?5:6;//数值可以变
System.out.println(c);
}
}
public class l1 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=a>b?10:20;//对应a与b
System.out.println(c);
}
}
7,位运算符
![](https://i-blog.csdnimg.cn/blog_migrate/cbae3d07aa69418e970691974d724668.png)