一、算数运算符
1、算术运算符:
+ - * / % ++ --
只展示++和--
int a = 5, b = 5, c = 5, e = 5;
++a;
b++;
int d = c++;
int f = ++e;
System.out.println(a);//6
System.out.println(b);//6
System.out.println(c);//6
System.out.println(d);//5
System.out.println(e);//6
System.out.println(f);//6
++号在变量的前面,先自增1再赋值给其他变量 ,例如第6行;++号在变量的后面,先赋值给其他变量,然后再自增1,例如第5行。
2、关系运算符:
> < >= <= == !=
结果为boolean类型,关系成立则为true,关系不成立则为false
3、逻辑运算符:
&(与) &&(短路与) |(或) ||(短路或) !(非)
&&:第一个条件为false,则不会检查后面的判断条件;如果是&,则会继续检查后面的条件
||:第一个条件为true,则不会检查后面的条件;如果是|,则会继续检查后面的条件
!: 对判断结果取反,结果为真,加上!,最后的结果就为假。反之亦然
4、赋值运算符:
= += -= *= /= %=
除了=号以外,其他的赋值运算符都自带强制转换类型的功能
5、字符串连接符:
+
String str1 = "hello";
String str2 = "world";
System.out.println(str1+str2);//helloworld
6、条件/三目运算符:
语法:判断表达式?返回值1:返回值2
判断表达式的结果为true,则返回值为“返回值1”;结果为false,则返回值为“表达式2”
int a = 1, b = 2;
int c = a < b? 3 : 4;
System.out.println(c);//3