逻辑运算符
逻辑运算符只能⽤于 boolean 类型的数据运算,判断 boolean 数据之间的逻辑关系,与、或、⾮。 &(与)、|(或)、!(⾮)、&&(短路与)、||(短路或)
public class OperatorDemo2 {
public static void main(String[] args){
int a=3;
int b=4;
int c=5;
与&
变量一&变量二:有false,则false
System.out.println((a>b)&(a<c));false
System.out.println((a>b)&(a>c));false
System.out.println((a<b)&(a<c));true
System.out.println((a<b)&(a>c));false
或|
变量一|变量二:有true,则true
System.out.println((a>b)|(a<c));true
System.out.println((a>b)|(a>c));false
System.out.println((a<b)|(a<c));true
System.out.println((a<b)|(a>c));true
非!
变量一!变量二:false=true,true=false
System.out.println(!(a>b));true
异或^
相同为false,不同为true
System.out.println((a>b)^(a<c));true
System.out.println((a>b)^(a>c));false
System.out.println((a<b)^(a<c));false
System.out.println((a<b)^(a>c));true
短路与&&
只要符号左边false,则右边表达式不在执行,直接返回false
boolean flag = (++a == 3) && (++b == 4);
System.out.println("a:"+a);4
System.out.println("b:"+b);4
短路或||
只要符号左边true,则右边表达式不在执行,直接返回true
boolean Flag = (++a == 4) || (++b == 4);
System.out.println("a:"+a);5
System.out.println("b:"+b);5
位运算符
&(按位与)、|(按位或)、^(按位异或)、 ~(按位取反) 、<<(左移)、 >>(右移) 、>>>(无符号右移位)
&
把变量转为二进制,一一对应,都为 1,则该位记做 1,否则记做 0
|
把变量转为二进制,一一对应,有为 1,则该位记做 1,否则记做 0
^
把变量转为二进制,一一对应,相同为0,不同为1
<<(左移)
<<:左边最高位丢弃,右边空缺补0 :将要移动的数扩大2^n n: 就是移动的位数
>>(右移)
>>:正数:向右移动,右边空缺位补0,负数:向右移动:右边空缺位补1
>>>(无符号右移位)
无论最高位0还1, 空缺位都补0
三目运算符
根据不同的条件给同⼀个变量赋不同的值,变量 = 条件?值1:值2.
package org.wdit.unit06;
public class OperatorDemo4 {
public static void main(String[] args){
int a =3;
int b =4;
int max;
System.out.println((a>b)? (max =a) :(max = b));
max =((a>b)?(a):(b));
System.out.println(max);
int x =3;
}
}
public class OperatorDemo4 {
public static void main(String[] args){
int a =3;
int b =4;
int max;
//System.out.println((a>b)? (max =a) :(max = b));
max =((a>b)?(a):(b));
System.out.println(max);
}
}
输出:4
package org.wdit.unit05;
public class OperatorDemo2 {
public static void main(String[] args){
int a=3;
int b=4;
int c=5;
//&:有false,则false
System.out.println((a>b)&(a<c));
System.out.println((a>b)&(a>c));
System.out.println((a<b)&(a<c));
System.out.println((a<b)&(a>c));
//|:有ture则ture
System.out.println((a>b)|(a<c));
System.out.println((a>b)|(a>c));
System.out.println((a<b)|(a<c));
System.out.println((a<b)|(a>c));
//!:false=ture
System.out.println(!(a>b));
//^:相同为false,不同为ture
System.out.println((a>b)^(a<c));
System.out.println((a>b)^(a>c));
System.out.println((a<b)^(a<c));
System.out.println((a<b)^(a>c));
//&&:只要符号左边false,则右边表达式不在执行,直接返回false
boolean flag = (++a == 3) && (++b == 4);
System.out.println("a:"+a);
System.out.println("b:"+b);
//||只要符号左边ture,则右边表达式不在执行,直接返回ture
boolean Flag = (++a == 4) || (++b == 4);
System.out.println("a:"+a);
System.out.println("b:"+b);
}
}
数据互换
package org.wdit.unit06;
public class OperatorDemo3 {
public static void main(String[] args){
//方式1
int a = 3;
int b = 4;
int c = a;
a = b ;
b = c ;
System.out.println("a"+a);
System.out.println("b"+b);
//方式二
a = a ^ b;
b = a ^ b;
a = a^b;
System.out.println("a"+a);
System.out.println("b"+b);
//方式三
a = a=+ b;
b = a-b;
a = a-b;
System.out.println("a"+a);
System.out.println("b"+b);
//方式四
b=(a+b)-(a=b);
System.out.println("a"+a);
System.out.println("b"+b);
}
}