突然有人问起逻辑运算符,再经参考发现原来有个重大思考误区,故留个代码输出加深印象。。。
public class LogicOpTest {
public static void main(String[] args) {
Expression[] exps = new Expression[]{ Expression.FALSE, Expression.TRUE };
for(int i=0, j=0;;){
if(i<exps.length){
if(j<exps.length){
invoke( exps[i], exps[j++]);
}else{
i++; j=0;
}
}
else break;
}
}
private static void invoke(Expression left, Expression right){
output( "&", left.returnValue() & right.returnValue() );
output( "&&", left.returnValue() && right.returnValue() );
output( "|", left.returnValue() | right.returnValue() );
output( "||", left.returnValue() || right.returnValue() );
output("");
}
private interface Expression{
boolean returnValue();
final Expression FALSE = new Expression() {
@Override public boolean returnValue() {
output("Expression: FALSE ");
return false;
}
};
final Expression TRUE = new Expression() {
@Override public boolean returnValue() {
output("Expression: TRUE");
return true;
}
};
}
private static void output(String msg){
System.out.println(msg);
}
private static void output(String msg, boolean value){
System.out.println("Result: "+msg+" "+value);
}
}
输出结果:
Expression: FALSE
Expression: FALSE
Result: & false
Expression: FALSE
Result: && false
Expression: FALSE
Expression: FALSE
Result: | false
Expression: FALSE
Expression: FALSE
Result: || false
Expression: FALSE
Expression: TRUE
Result: & false
Expression: FALSE
Result: && false
Expression: FALSE
Expression: TRUE
Result: | true
Expression: FALSE
Expression: TRUE
Result: || true
Expression: TRUE
Expression: FALSE
Result: & false
Expression: TRUE
Expression: FALSE
Result: && false
Expression: TRUE
Expression: FALSE
Result: | true
Expression: TRUE
Result: || true
Expression: TRUE
Expression: TRUE
Result: & true
Expression: TRUE
Expression: TRUE
Result: && true
Expression: TRUE
Expression: TRUE
Result: | true
Expression: TRUE
Result: || true
【20130904更新】扩充null值的对比
public class LogicOpTest {
public static void main(String[] args) {
Expression[] exps = new Expression[]{
Expression.FALSE, Expression.TRUE, Expression.NULL
};
for(int i=0, j=0;;){
if(i<exps.length){
if(j<exps.length){
invoke( exps[i], exps[j++]);
}else{
i++; j=0;
}
}
else break;
}
}
private static void invoke(final Expression left, final Expression right){
abstract class Invoker{
private final String name;
public Invoker(String name) {
this.name = name;
}
final void invoke() {
try {
output( this.name, this.doInvoke() );
} catch (NullPointerException e) {
output( this.name, "caught NullPointerException" );
}
}
abstract boolean doInvoke() ;
}
new Invoker("&" ){boolean doInvoke(){
return left.returnValue() & right.returnValue() ;}}.invoke();
new Invoker("&&"){boolean doInvoke(){
return left.returnValue() && right.returnValue() ;}}.invoke();
new Invoker("|" ){boolean doInvoke(){
return left.returnValue() | right.returnValue() ;}}.invoke();
new Invoker("||"){boolean doInvoke(){
return left.returnValue() || right.returnValue() ;}}.invoke();
output("");
// {
// String s = null;
// for(int i=0;;){
// try{
// if(i==0){ s = "&"; output( s, left.returnValue() & right.returnValue() ); i++; }
// if(i==1){ s = "&&"; output( s, left.returnValue() && right.returnValue() ); i++; }
// if(i==2){ s = "|"; output( s, left.returnValue() | right.returnValue() ); i++; }
// if(i==3){ s = "||"; output( s, left.returnValue() || right.returnValue() ); i++; }
// break;
// }catch(NullPointerException e){
// output( s, "caught NullPointerException" ); i++;
// }
// }
// output("");
// }
}
private interface Expression{
Boolean returnValue();
final Expression FALSE = new Expression() {
@Override public Boolean returnValue() {
output("Expression: FALSE ");
return Boolean.FALSE;
}
};
final Expression TRUE = new Expression() {
@Override public Boolean returnValue() {
output("Expression: TRUE");
return Boolean.TRUE;
}
};
final Expression NULL = new Expression() {
@Override public Boolean returnValue() {
output("Expression: NULL");
return null;
}
};
}
private static void output(String msg){
System.out.println(msg);
}
private static void output(String msg, boolean value){
System.out.println("Result: "+msg+" "+value);
}
private static void output(String msg, String msg2){
System.out.println("Result: "+msg+" "+msg2);
}
}
输出结果:
Expression: FALSE
Expression: FALSE
Result: & false
Expression: FALSE
Result: && false
Expression: FALSE
Expression: FALSE
Result: | false
Expression: FALSE
Expression: FALSE
Result: || false
Expression: FALSE
Expression: TRUE
Result: & false
Expression: FALSE
Result: && false
Expression: FALSE
Expression: TRUE
Result: | true
Expression: FALSE
Expression: TRUE
Result: || true
Expression: FALSE
Expression: NULL
Result: & caught NullPointerException
Expression: FALSE
Result: && false
Expression: FALSE
Expression: NULL
Result: | caught NullPointerException
Expression: FALSE
Expression: NULL
Result: || caught NullPointerException
Expression: TRUE
Expression: FALSE
Result: & false
Expression: TRUE
Expression: FALSE
Result: && false
Expression: TRUE
Expression: FALSE
Result: | true
Expression: TRUE
Result: || true
Expression: TRUE
Expression: TRUE
Result: & true
Expression: TRUE
Expression: TRUE
Result: && true
Expression: TRUE
Expression: TRUE
Result: | true
Expression: TRUE
Result: || true
Expression: TRUE
Expression: NULL
Result: & caught NullPointerException
Expression: TRUE
Expression: NULL
Result: && caught NullPointerException
Expression: TRUE
Expression: NULL
Result: | caught NullPointerException
Expression: TRUE
Result: || true
Expression: NULL
Result: & caught NullPointerException
Expression: NULL
Result: && caught NullPointerException
Expression: NULL
Result: | caught NullPointerException
Expression: NULL
Result: || caught NullPointerException
Expression: NULL
Result: & caught NullPointerException
Expression: NULL
Result: && caught NullPointerException
Expression: NULL
Result: | caught NullPointerException
Expression: NULL
Result: || caught NullPointerException
Expression: NULL
Result: & caught NullPointerException
Expression: NULL
Result: && caught NullPointerException
Expression: NULL
Result: | caught NullPointerException
Expression: NULL
Result: || caught NullPointerException