优先级 | 运算符 | 结合性 |
---|---|---|
1 | ( ) [ ] . | 从左到右 |
2 | ! ~ ++ – | 从右到左 |
3 | * / % | 从左到右 |
4 | + - | 从左到右 |
5 | << >> >>> | 从左到右 |
6 | < <= > >= instanceof | 从左到右 |
7 | == != | 从左到右 |
8 | & | 从左到右 |
9 | ^ | 从左到右 |
10 | | | 从左到右 |
11 | && | 从左到右 |
12 | || | 从左到右 |
13 | ? : | 从左到右 |
14 | = += -= *= /= %= &= |= ^= ~= <<= >>= >>>= | 从右到左 |
15 | , | 从右到左 |
//二元运算符
int a =10;
int b =20;
long c =2523423L;
short d =25;
byte e=8;
System.out.println(a+b);//30
System.out.println(a-b);//-10
System.out.println(a*b);//200
System.out.println(a/(double)b);//0.5
System.out.println(b+c+d+e);//long
System.out.println(b+d+e);//int
System.out.println(d+e);//int
//关系运算符,正确或错误,布尔值
System.out.println(a>b);//false
System.out.println(a<b);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
//自增,自减,一元运算符
System.out.println("===================");
int x=2;
System.out.println(x);//2
int y=x++;//先执行再自增
System.out.println(x);//3
int z=++x;//先自增再执行
System.out.println(x);//4
System.out.println(y);//2
System.out.println(z);//4
System.out.println(x);//4
//幂运算,用工具类来操作
double pow =Math.pow(2,5);
System.out.println(pow);//32.0
//逻辑运算符,与或非
System.out.println("---------------------");
boolean m=true;
boolean n=false;
System.out.println("m&&n:"+(m&&n));//false
System.out.println("m||n:"+(m||n));//true
System.out.println("!(m&&n):"+!(m&&n));//true
//短路运算
int w=3;
boolean q=(w<2)&&(w++<4);
System.out.println(w);//3
System.out.println(q);//false
//字符串链接符, + ,String
System.out.println("=========================");
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30
//三元运算符 x?y:z 如果x为真,结果是y否则为z
int score = 70;
String type = score<60?"不及格":"及格";
System.out.println(type);//及格
学习地址:https://www.bilibili.com/video/BV12J41137hu?p=30
原文:https://www.cnblogs.com/bangblog/p/14593610.html