java中的运算符
文章目录
- 运算符
- 逻辑运算符扩展
- 位运算符扩展
运算符
java语言支持如下运算符
- 算术运算符:+,-,*,/,%,++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&,|,^,~,>>,<<,>>>
- 条件运算符:?:
- 扩展赋值运算符:+=,-=,*=,/=
逻辑运算符扩展
public class Demo() {public static void main(String[] args) {// 与(and) 或(or) 非(取反)boolean a = true;boolean b = false;System.out.println("a&&b:"+(a&&b)); // 逻辑与运算:两个变量都为真,结果才为trueSystem.out.println("a||b:"+(a||b)); // 逻辑或运算:两个变量中有一个为真,则结果就为真System.out.println("!(a&&b):"+!(a&&b)); // 如果是真,则变为假,如果是假,则变为真// 短路运算int c1 = 5;boolean d1 = (c1<4)&&(c1++<4);System.out.println(d1); // falseSystem.out.println(c1); // 5int c2 = 5;boolean d2 = (c2<10)||(c2++<5);System.out.println(d2); // trueSystem.out.println(c2); // 5 }
}
位运算符扩展
public class Demo() {public static void main(String[] args) {/*A = 0011 1100B = 0000 1101--------------------A&B = 0000 1100A|B = 0011 1101a^b = 0011 0001~b = 1111 0010<< * 2>> / 20000 0000 00000 0000 10000 0010 20000 0011 30000 0100 40000 1000 80001 0000 16System.out.println(2<<3) // 16*/}
}