1.关系运算符
public class Test {
public static void main(String[] args) {
int a=3;
int b=5;
boolean b1,b2,b3,b4;
b1=a==b;//false
b2=a!=b;//true
b3=a>b;//false
b4=a<b;//true
System.out.println(b1+","+b2+","+b3+","+b4);
}
}
2.逻辑运算符
与 运算符:所有条件都为真,则结果才为真,只要有一个假,结果就是假
或 运算符:只要有一个条件为真,结果就是真,所有条件都为假,结果才为假
boolean b;
b=condition1&condition2;//先求得1,2的值,然后进行判断
b=comdition1&&condition2;//首先对condition进行判断,如果为真,再判断condition2,如果condition1为假,则不再对condition2进行判断
public class Test2 {
//调用该方法的时候会返回true
public static boolean returntrue() {
System.out.println("return true");
return true;
}
//调用该方法的时候会返回false
public static boolean returnfalse() {
System.out.println("return false");
return false;
}
public static void main(String[] args) {
System.out.println("逻辑与运算 returntrue()&returnfalse()");
boolean b1=returntrue()&returnfalse();
System.out.println(b1);
System.out.println("短路与运算 returntrue()&&returnfalse()");
boolean b2=returntrue()&&returnfalse();
System.out.println(b2);
System.out.println("短路与运算 returntrue()&&returnfalse()");
boolean b3=returnfalse()&&returntrue();
System.out.println(b3);
System.out.println("逻辑或运算 returntrue()|returnfalse()");
boolean b4=returntrue()|returnfalse();
System.out.println(b4);
System.out.println("短路与运算 returntrue()||returnfalse()");
boolean b5=returntrue()||returnfalse();
System.out.println(b5);
System.out.println("短路与运算 returntrue()||returnfalse()");
boolean b6=returnfalse()||returntrue();
System.out.println(b5);
}
}
3.三元运算符
public class Test3 {
public static void main(String[] args) {
int i,k,j;
i=5;
j=9;
k=i>=j?i:j;
System.out.println(k);
}
}