Fifth Chapter Java Operators

Source: Internet
Author: User
Tags arithmetic operators logical operators

Java operator One classification
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • logical operators
    • Conditional operators
Two arithmetic operators

Arithmetic operators are primarily used for basic arithmetic operations, such as addition, subtraction, multiplication, division, and so on.

Arithmetic operators commonly used in Java:

Among them, + + and--can appear on the left side of the operand, or on the right, but the result is different.

That

Example 1:

int a = 1; int b = ++a; // let a do the self-increment first, and then assign the value to B System.out.println (a); System.out.println (b);

The result of operation is 2;

Example 2:

int a = 1; int b = a++; // A first value is assigned to B, and then self-increment is performed System.out.println (a); System.out.println (b);

The results are: 2 and 1.

Be sure to pay attention! The increment and decrement operators can only be used for operation variables and cannot be used directly to manipulate values or constants! For example, 5++, 8--and so on are all wrong!

The ps:% is used to find the remainder, also known as the modulo operator.

Three-assignment operators

An assignment operator is a symbol that specifies a numeric value for a variable or constant. If you can use "=" to assign the result of the right expression to the left operand.

The assignment operators commonly used in Java are shown in the following table:

Four comparison operators

The comparison operator is used to determine the size of two data, for example: greater than, equal to, not equal to. The result of the comparison is a Boolean value (True or false).

The common comparison operators in Java are shown in the following table:

Attention:

    • >, <, >=, <= only support the left and right sides of the operand is a numeric type;
    • The operands on both sides of = =,! = can be either numeric types or reference types.
Five logical operators

Logical operators are primarily used for logical operations. The logical operators commonly used in Java are shown in the following table:

Here is an example of the MU-net:

We can understand logical operators from the perspective of "voting":

1, and : Ask everyone to vote for consent, to pass an issue

2, or : Just ask a person to vote for permission to pass an issue

3, non : Someone originally voted to agree, through the non-operator, you can make their votes invalid

4. XOR : There is only one person who can vote to approve the issue.

When using logical operators, we encounter a very interesting " short circuit " phenomenon.

For example: (One > both) && (one < three), if you can determine that the left one > The run result is false, then the system thinks there is no need to execute the one < three on the right.

Similarly, in (One > both) | | (One < three), if you can determine that the left expression will run as true, the system will also assume that it is no longer necessary to perform the one < three on the right.

Six conditional operators

The conditional operator (? :) also known as the ternary operator.

Syntax form: boolean expression? Expression 1: Expression 2

Operation procedure: Returns the value of expression 1 If the value of the Boolean expression is true , otherwise returns the value of expression 2

For example:

Because the value of the expression 8>5 is true, the return: 8 is greater than 5

Precedence of seven operators

The so-called priority is the order of operations in an expression. The precedence of operators commonly used in Java is shown in the following table:

Level 1 has the highest priority, and level 11 has the lowest priority . For example, the result of x = 7 + 3 * 2 is 13 "Multiply First and add"!

PS: No need to go to the order of precedence of the rote operators, in real development, the use of parentheses is used to assist in priority management. For example:

Analysis: Parentheses have the highest precedence, so

1, execute a + 18, the result is 30

2, perform (A + 18)% 4 modulo, the result is 2

3, execute A * ((A + 18)% 4), the result is 24

Fifth Chapter Java Operators

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.