Java Logical operator Understanding

Source: Internet
Author: User
Tags logical operators

1.1 logical Operators

The logical operator, which is used for Boolean operations, and the final result of the operation is a Boolean value of True or false.

Operator

Arithmetic rules

Example

Results

&

And

False&true

False

|

Or

False|true

True

^

XOR or

True^flase

True

!

Non -

!true

Flase

&&

Short Circuit and

False&&true

False

||

Short Circuit or

false| | True

True

After reading the graph, let's look at the general use of logical operators:

L logical operators usually concatenate two other expressions to evaluate the result of a Boolean value

L when the use of short-circuit and or short-circuit or, as long as the results can be judged, the back part is no longer judged.

int x = 1,y = 1;

if (x++==2 & ++y==2)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);

& and, go through the conditions, regardless of the outcome of the right and wrong. The loop body is entered when the condition is satisfied.

int x = 1,y = 1;

if (x++==2 && ++y==2)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);

&& Short circuit and, a short circuit will not go, if the first result is false, then the back will not go, the direct end (jump out of the loop), if not go down.

int x = 1,y = 1;

if (x++==1 | ++y==1)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);

| or, as long as there is a condition to be satisfied, enter the loop body, and go through the whole condition

int x = 1,y = 1;

if (x++==1 | | ++y==1)
{
x = 7;
}
System.out.println ("x=" +x+ ", y=" +y);

|| Short circuit or, as long as there is a condition to not go behind, such as the first meet the conditions, do not go behind, directly into the loop body.

Java Logical operator Understanding

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.