Java programming things 23-logical operators

Source: Internet
Author: User

Java programming those things 23-logical operators Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 4.3 logical operatorsLogical operators are symbols used for logical operations. Logical operations include and, or (OR) and non-(not). In a program, they are used to connect multiple conditions to form more complex conditions. The result of logical operators is of the boolean type. The data involved in logical operations must also be of the boolean type. For the types and descriptions of logical operators, see table 4-3. Table 4-3 logical operators
Symbol Name Function Description
&& Logic and True only when both conditions are true; otherwise, false.
| Logic or If either of the two conditions is true, true is used; otherwise, false is used.
! Non-logical Operate on only one data and reverse the data
Example code used for logical operators: Boolean b1 = true; Boolean b2 = false; Boolean B3 = B1 & B2; // The value of B3 is false B3 = b1 | B2; // The B3 value is true B3 =! B1; // The value of B3 is false in the actual program. You can use the corresponding logical operator number as needed. Actual use example: L indicates whether the Variable N belongs to the [) interval int N = 4; Boolean B = (n> = 0) & (n <10 ); for Variable N, As long as N meets the conditions greater than or equal to zero and less than 10 at the same time, it is located in the [) range, because the program cannot write conditions such as 0 <= n <10, you must use logical operators to connect. L indicates that the variable n does not belong to the [) range. One method is: int N = 4; Boolean B =! (N> = 0) & (n <10); here, the condition that belongs to the specified range is reversed, and the condition that does not belong to the specified range is obtained. Another method is: int N = 4; Boolean B = (n <0) | (n> = 10); here we make a simple work ing, if the Variable N does not belong to this interval, you only need to meet any mathematical condition where N is less than 0 or N is greater than or equal to 10, such or relationship is implemented in a program using logic or implementation. In program design, the corresponding logical operators can be used to implement relatively complex combination conditions based on logical needs, so as to implement the functions of the corresponding program. Finally, let's talk about the difference between & and &. In fact, you can use & or use & in logic and operation, and there is no difference in function. The difference between the two is that, for &, if the left condition is false, the value of the right condition is also calculated. For &, if the left condition is false, the condition on the right is not calculated. This phenomenon is called short circuit. Sample Code: int n =-1; Boolean b1 = (n> = 0) & (n <10); Boolean b2 = (n> = 0) & (n <10); for the second line of code, both conditions are calculated. For the third line of code, because n> = 0 is not true, then n <10 is not executed at all. Of course, the two get the same final result. For the current Code, the difference is not big, but if the subsequent condition is a method (the concept of the method will be introduced later), it will affect the program logic. Sample Code for verification & features: public class test {public static void main (string [] ARGs) {int n = 10; Boolean B = (n <8) & (n = 1 )! = 0); int M = 20; Boolean b1 = (M <8) & (M = 1 )! = 0); system. Out. println (n); system. Out. println (m) ;}finally, EDIT: corrected and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.