Introduction to Java first quarter chapter III common operators

Source: Internet
Author: User
Tags arithmetic operators

This is my lesson Web Java course notes, the original video link is: http://www.imooc.com/learn/85

3-1 what is an operator

An operator is a "function" symbol that informs Java about the operation. For example, if we need to set the value of the variable age to 20, we need a "=" to tell the program that it needs to be assigned. The common operators in the Java language can be categorized as follows:? Arithmetic operators
? assignment operators
? comparison operators
? logical operators
? Conditional operators

3-2 arithmetic operators in Java
Arithmetic operators commonly used in Java:

where + + and--can appear on the left side of the operand, or on the right, but the result is different
Example 1:
Operation Result:

Example 2:
Operation Result:

It is important to note that 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.
3-3 assignment operators in Java
Common assignment operators supported by Java, as shown in the following table:


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


Attention! The ①>,<,>=,<= only supports the left and right sides of the operand is Numeric Type②==,! = operands on both sides can be numeric types or reference types.
3-5 logical operators in Java
The logical operators commonly used in Java are shown in the following table:


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.


3-6 conditional operators in Java

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


3-7 Precedence of operators in Java
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: We don't have to go. The order of precedence of the rote operators, which is typically used to assist in priority management in real-world development. 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


Introduction to Java first quarter chapter III common 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.