Java Logical Operations

Source: Internet
Author: User

-----------siwuxie095

Logical operation is an operation on the basis of relational operation, which can deal with more complicated problems.

The result of the logical operation is true or false

Types of logical operations:

1. Logic and Operation:

(1) long Road and Operation

The operation symbol of long road and Operation:&

& Both sides are integers, is the bitwise and operation (binary);& on both sides of the relationship operation, is the logic and Operation

Long distances and operations calculate the results of the relational operations on both sides.

Such as:

(2) Short circuit and Operation

Operation Symbol:&& for short circuit and operation

The result of the relational operation to the left of the operator is false , the relational operation on the right is no longer

Calculation (this calculation feature can improve the efficiency of the program, and is safe)

Such as:

2. Logic or operation:

(1) long road or Operation

Operators for long distances or operations: |

| is a bitwise OR operation (binary) when both sides are integers;| on either side of a relational operation, is a logical or an operation

A long road or operation calculates the results of the relational operations on both sides.

Such as:

(2) Short circuit or Operation

Operator of short Circuit or operation: | |

When the relationship operation on the left side of the operation symbol evaluates to true, the relational operation on the right is no longer performed.

Direct result of True (also improves computational efficiency)

Such as:

3. Logical non-operation:

Such as:

4, the logical operation priority:

such as: (Note: The precedence of relational operations is higher than all logical operations)

Program Application: Keyboard input a 5- point score to determine whether the number is 5- point score

Code:

package com.siwuxie095.test;

import java.util.Scanner;

Public class Test18 {

Public static void main (string[] args) {

Scanner scanner=new Scanner (System. In);

System.out.println (" Please enter 5 score:");

int score=scanner.nextint ();

// and arithmetic

boolean isfivescore=score>=0&&score<=5;

// Non-operational

//boolean isfivescore=! ( score<0| | SCORE>5);

System.out.println (score+" is a 5 -point score:"+isfivescore);

// or operations

//boolean isnotfivescore=score<0| | score>5;

//system.out.println (score+ " is not a 5 -point score:" +isnotfivescore);

}

}

In Eclipse:

Run at a glance:

"Made by siwuxie095"

Java Logical Operations

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.