基本运算符
运算符
- 算数运算符:+,-,*,/,%,++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=,instanseof
- 逻辑运算符:&&与,||或,!非
- 位运算符:&,|,^,~,>>,<<,>>>
- 条件运算符:?:
- 扩展赋值运算符:+=,-=,*=,/=
算数运算符
package operater;
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//ctrl+D——复制当前行到下一行
int a=10;
int b=20;
int c=25;
int d=25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);//0.5 强转
}
}
30
-10
200
0.5
赋值运算符
package operater;
public class Demo2 {
public static void main(String[] args) {
long a = 123123132132232L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
}
}
123123132132373
141
18
关系运算符
package operater;
public class Demo3 {
public static void main(String[] args) {
//关系运算符的返回结果:true,false
//if
int a = 10;
int b = 20;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
false
true
false
true
一元运算符
package operater;
public class Demo4 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算符
int a = 3;
int b = a++;//执行完代码后,先给b赋值,再自增
//a++ a=a+1
System.out.println(a);
//++a a=a+1
int c = ++a;//执行完代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(a);
System.out.println(b);
System.out.println(b);
System.out.println(c);
System.out.println(c);
//幂运算
double pow = Math.pow(2, 3);//快捷键Alt+Enter引入变量
System.out.println(pow);
}
}
逻辑运算符
package operater;
public class Demo5 {
public static void main(String[] args) {
//逻辑运算符
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b = "+(a&&b));//逻辑与运算:二者都为真
System.out.println("a || b = "+(a||b));//逻辑或运算:二者有一为真
System.out.println("!(a && b) = "+!(a&&b));//真假交换
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);//后面没有执行
System.out.println(d);
System.out.println(c);
}
}
a && b = false
a || b = true
!(a && b) = true
false
5
位运算符
package operater;
public class Demo6 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
---------------------
A&B=0000 1100
A|B=0011 1101
A^B=0011 0001
~B =1111 0010
2*8 = 16 2*2*2*2
<< 左移 *2
>> 右移 /2
效率高
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
16
扩展运算符
package operater;
public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b;//a=a+b
a -= b;//a=a-b
System.out.println(a);
//字符串连接符 + ,String ""后会当成字符串
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
10
30
1020
30
三元运算符
格式:x ? y : z
如果x==true,则结果为y,否则为z
package operater;
public class Demo8 {
public static void main(String[] args) {
//三元运算符
//x ? y : z
//如果x==true,则结果为y,否则为z
int score = 80;
String type = score< 60 ? "不及格" : "及格";
System.out.println(type);
}
}
及格
运算符优先级
类别 | 操作符 | 关联性 |
---|---|---|
后缀 | () [] . (点操作符) | 左到右 |
一元 | expr++ expr– | 从左到右 |
一元 | ++expr --expr + - ~ ! | 从右到左 |
乘性 | * /% | 左到右 |
加性 | + - | 左到右 |
移位 | >> >>> << | 左到右 |
关系 | > >= < <= | 左到右 |
相等 | == != | 左到右 |
按位与 | & | 左到右 |
按位异或 | ^ | 左到右 |
按位或 | | | 左到右 |
逻辑与 | && | 左到右 |
逻辑或 | | | | 左到右 |
条件 | ?: | 从右到左 |
赋值 | = + = - = * = / =%= >> = << =&= ^ = | = | 从右到左 |
逗号 | , | 左到右 |