Java的布尔运算「专栏补充」

你好,我是悦创。

其实,学完 Python (其实没学完,知识点太多,库很多),然后入坑 Java (谁叫学校教呢,为了让知识点跟上时代和实际开发才有了这个系列的笔记),好了,废话不多说。直接开始记笔记!

1. 布尔运算符

这里强调一点,根据一个具有实际开发十几年经验的前辈所得来的经验,能用 且且、或或 运算符,尽量使用 且且、或或 ,具体原因先看如下代码:

/*
 * project = 'Java_Coder', file_name = '笔记', author = 'AI悦创'
 * time = '2020/5/8 下午6:15', product_name = IntelliJ IDEA, 公众号:AI悦创
 * code is far away from bugs with the god animal protecting
 *    I love animals. They taste delicious.
 */

public class notes {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		System.out.println(a & b); // & 且 (and)
		System.out.println(a && b); // &$ 且且 (andand)
		System.out.println(a | b); // | 或 (or)
		System.out.println(a || b); // | 或或 (oror)
		// 对比
		System.out.println(a || (10 / 0 > 1));
		System.out.println(a | (10 / 0 > 1));
	}
}

上面的代码,我们一部分一部分拿出来讲解:

1.1 第一部分

public class notes {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		System.out.println(a & b); // & 且 (and)
		System.out.println(a && b); // && 且且 (andand)
	}
}

a & b (a and b)的运行逻辑是这样的:

  1. 它会先查看 a 值的真假,如果 a 是假的,那程序就不会继续判断 b 的真假——返回:false
  2. 如果 a 是真的,则会继续判断 b 的真假,如果 b 的值为真则返回:true,反之为:false
  3. 在这个部分,&& 的作用不好讲解,所以这里需要你继续往下看。

所以,上面的运行结果如下:

false
false

1.2 第二部分

/*
 * project = 'Java_Coder', file_name = '笔记', author = 'AI悦创'
 * time = '2020/5/8 下午6:15', product_name = IntelliJ IDEA, 公众号:AI悦创
 * code is far away from bugs with the god animal protecting
 *    I love animals. They taste delicious.
 */

public class notes {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		System.out.println(a | b); // | 或 (or)
		System.out.println(a || b); // | 或或 (oror)
	}
}

a | b (a or b)的运行逻辑是这样的:

  1. 它会先判断 a 是真是假,如果 a 是真的则会继续判断 b 的真假,b 为真返回结果回——trueb 为假返回的也是——true;(这个地方它会返回警告,这里警告信息,在下面第三部分输出演示)
  2. 如果 a 是假的,也会继续判断 b 的真假,如果 b 为真,则返回——true,反之为——false

a || b (a oror b)的运行逻辑是这样的:

  1. 它会先判断 a 的真假,与上面其实类似,但是多了一个一个功能,就是:如果 a 为真,程序就不会再判断 b 的真假,其实也就是做了进一步优化。(上面的 a|b 虽然对结果没有影响,但还要多运行计算就有点浪费运行资源了)

综上:实际开发中多用 ||&&

1.3 第三部分

这样第三部分的代码就是为了让大家更加清晰的了解到,运行结果会出现一个警告(提示),代码如下:

public class notes {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;

		// 对比
		System.out.println(a || (10 / 0 > 1));
		System.out.println(a | (10 / 0 > 1));
	}
}

我们可以把上面的代码,分开运行为了更好观察输出结果:

public class notes {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;

		// 对比
		System.out.println(a | (10 / 0 > 1));
	}
}

运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at BigNumber.main(BigNumber.java:14)

这里我故意把 b 的值变成错误(也就是假)如果 b 得值设为 false 就不是输出警告,这里修改成错误的原因就是为了证明程序有判断 b 位置值得真假。

接下来我们再来看看,下面代码:

public class BigNumber {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		// 对比
		System.out.println(a || (10 / 0 > 1));
	}
}

运行结果:

true

通过这个例子,你就了解了 或或、且且 得具体有点了。

1.4 回顾

这回来看这个 andand 就知道了,如果 a 是 false,就不会继续判断 b 位置得真假。

/*
 * project = 'code005', file_name = 'BigNumber', author = 'AI悦创'
 * time = '2020/5/8 10:10', product_name = IntelliJ IDEA, 公众号:AI悦创
 * code is far away from bugs with the god animal protecting
 *    I love animals. They taste delicious.
 */

public class BigNumber {
    
    
	public static void main(String[] args){
    
    
		boolean a = false;
		boolean b = false;
		
		// 对比
		System.out.println(a && (10 / 0 > 1));
	}
}

运行结果:

false

如果 a 为真则继续判断,代码如下:

public class BigNumber {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		// 对比
		System.out.println(a && (10 / 0 > 1));
		System.out.println("Is runing");
	}
}

运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at BigNumber.main(BigNumber.java:14)

如果这个输出后面还有程序运行,则不会输出:

public class BigNumber {
    
    
	public static void main(String[] args){
    
    
		boolean a = true;
		boolean b = false;
		
		// 对比
		System.out.println(a && (10 / 0 > 1));
		System.out.println("Is runing");
	}
}

运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at BigNumber.main(BigNumber.java:14)

猜你喜欢

转载自blog.csdn.net/qq_33254766/article/details/108682947