java运算符优先级

运算符

算术运算

+、-、*、/

int x = 1;
int y = 2;
System.out.println(y++-++x+y++/--x*2);    //6
System.out.println(x);    //1
System.out.println(y);    //4

关系运算

<、<=、>、>=、==、!=

int x = 1;
int y = 2;
System.out.println(y++-++x+y++/--x*2 != 2+x*y);    //false

三目运算

int x = 1;
int y = 3;
System.out.println(x++>y++?x++:y++);    //4
System.out.println(x);    //2
System.out.println(y);    //5

逻辑运算

&&、&、|、||、!

int x = 1;
int y = 2;
System.out.println(y++-++x+y++/--x*2 != 2+x*y || ++x*2 == y);    //true
System.out.println(x);    //2
System.out.println(y);    //4

位运算

&、|、~(非)、^(异或)、>>、<<、>>>

int x = 12;
int y = 9;
System.out.println(x&y);    //8
System.out.println(x|y);    //13
System.out.println(x^y);    //5
System.out.println(~x);    //-13
System.out.println(1<<3);    //8

综合

int x = 1;
int y = 2;
System.out.println(1<<3+--y == 16 && x == y);    //true

算术运算、关系运算、逻辑运算、位运算的执行顺序

算术运算和位运算操作的是数,结果也是数

关系运算操作的是数(或者true/false),结果是true/false

逻辑运算操作true/false,结果true/false

所以算术运算和位运算优先关系运算,关系运算优先逻辑运算,至于数学运算和位运算的优先级,经过测试发现数学运算优先位运算

所以:算术运算>位运算>关系运算>逻辑运算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值