Boolean operations in Java "Column Supplement"

Hello, I am Yuechuang.

In fact, after learning Python (actually not finished, too many knowledge points, a lot of libraries), and then into the pit of Java (who calls the school to teach, in order to keep the knowledge points up to the times and actual development, there is this series of notes), alright, do not piffle any more. Just start taking notes!

1. Boolean operators

Here emphasize that, in accordance with a resulting senior actual development experience of ten years of experience, it can be used and and, or, or operator, to make use of and, and, or, or , look at the specific reasons for the following code:

/*
 * 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));
	}
}

The above code, we will explain part by part:

1.1 Part One

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)
	}
}

The operating logic of a & b (a and b) is as follows:

  1. It will first see a true or false value, if a is false, then the program will not continue to judge the genuineness b - Returns: false ;
  2. If a is true, it will continue to judge whether b is true or false, and if the value of b is true, return: true , otherwise: false ;
  3. In this part, the role of && is not easy to explain, so you need to continue to look down here.

Therefore, the above results are as follows:

false
false

1.2 Part Two

/*
 * 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)
	}
}

The operating logic of a | b (a or b) is as follows:

  1. It will first determine a true or false, if a true judgment will continue to b true and false, b is true returns results back - to true , b is false is returned - to true ; (this place it will return a warning , This warning message, output in the third part of the following demonstration)
  2. If a is false, it will continue to judge whether b is true or false. If b is true, it will return - true , otherwise - false ;

The operating logic of a || b (a oror b) is as follows:

  1. It will first determine a true and false, and in fact similar to the above, but more a function, that is: if a is true, the program will no longer judge b of true and false, in fact, is made further optimized. (Although the above a|b has no effect on the result, it would be a waste of running resources to run more calculations)

In summary: multi-purpose ||in actual development ,&&

1.3 Part Three

In this way, the third part of the code is to let everyone know more clearly that a warning (prompt) will appear in the running result, the code is as follows:

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));
	}
}

We can run the above code separately in order to better observe the output:

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

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

operation result:

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

Here I deliberately changed the value of b to an error (that is, false). If the value of b is set to false, it is not a warning. The reason for changing it to an error is to prove that the program has judged that the position of b is true or false.

Next, let's take a look again, the following code:

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

operation result:

true

Through this example, you would understand or or and and get a little specific.

1.4 Review

Looking at this andand this time, you will know that if a is false, it will not continue to judge whether the position b is true or false.

/*
 * 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));
	}
}

operation result:

false

If a is true, continue to judge, the code is as follows:

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");
	}
}

operation result:

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

If there is a program running after this output, it will not output:

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");
	}
}

operation result:

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

Guess you like

Origin blog.csdn.net/qq_33254766/article/details/108682947