运算符
优先级()
-
算数运算符: +,-,*,/,%, ++,–(比较重要)
-
赋值运算符 =
-
关系运算符:>, <, >=, <=, ==, !=instanceof
-
逻辑运算符:&&,||,!
-
位运算符:&,|,^, ~, >>, <<, >>>(了解!!!)
-
条件运算符 ?:
-
扩展赋值运算符:+=,-=,*=,/=
自增,自减
自增(int a = b++):在执行时,先将值赋给a,b再进行自增
(int a = ++b):在执行时,b先自增,再将值赋给a
自减:同理
逻辑运算
package Operator; /** * 逻辑运算符 */ public class Demo05 { 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); } }
位运算
package Operator; /** * 位运算符 */ public class Demo06 { public static void main(String[] args) { /* A = 0011 1100 B = 0000 1101 ------------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 异或 相同为0 不同为1 ~B = 1111 0010 */ /* 2*8=16 2*2*2*2 效率极高 << 左移 *2 >> 右移 /2 */ System.out.println(2<<3); } }
扩展赋值运算符,字符串连接符
package Operator; /** * 扩展赋值运算符,字符串连接符 */ public class Demo07 { 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); //1020 System.out.println(a+""+b); //1020 System.out.println(a+b+""); //30 } }
三元运算符
package Operator; /** * 三元运算符 */ public class Demo08 { public static void main(String[] args) { //?: //x?y:z //如果x为true,则结果为y,否则为z int score = 50; String type = score<60?"不及格":"及格"; //必须掌握 //if System.out.println(type); } }
https://www.bilibili.com/read/cv5702420?spm_id_from=333.999.0.0
这个视频里的老师讲得很好。
免费!!!