逻辑运算符……无聊小代码

突然有人问起逻辑运算符,再经参考发现原来有个重大思考误区,故留个代码输出加深印象。。。


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



转载于:https://my.oschina.net/adan1/blog/156621

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值