Home  >  Article  >  Java  >  What are the java operators?

What are the java operators?

藏色散人
藏色散人Original
2020-12-30 11:04:1927219browse

Java operators include: 1. Arithmetic operators, such as ", -, %, etc."; 2. Assignment operators, such as "="; 3. Comparison operators, such as "greater than, equal to, not Equal to"; 4. Logical operators, such as "OR, AND, NOT"; 5. Conditional operators, such as "ternary operator" and so on.

What are the java operators?

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer. This method is suitable for all brands of computers.

Recommended: "java Video Tutorial"

The operator is a "functional" symbol that is used to notify Java to perform related operations.

Commonly used operators in Java language can be divided into the following types:

Ø Arithmetic operators

Ø Assignment operators

Ø Comparison operators

Ø Logical operators

Ø Conditional operators

1. Arithmetic operators

Main arithmetic operators Used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, etc.

Commonly used arithmetic operators in Java:

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

a: assign value first, then increment

a: increment first, then assign value

The auto-increment and auto-decrement operators can only be used for operations Variables cannot be used directly to operate on values ​​or constants! For example, 5, 8-- are all wrong!

2. Assignment operator

The assignment operator refers to the symbol that specifies a numerical value for a variable or constant. For example, you can use "=" to assign the expression result on the right to the operand on the left.

The common assignment operators supported by Java are as shown in the following table:

3. Comparison operators

Comparison operators are used to determine the size of two data, such as: greater than, equal to, not equal to. The result of the comparison is a Boolean value ( true or false ).

Commonly used comparison operators in Java are shown in the following table:

Note:

1, > , < , >=, <= only supports the left and right operands to be numeric types

2, ==, != The operands on both sides can be either numeric types or reference types

4. Logical operators

Logical operators are mainly used to perform logical operations. Commonly used logical operators in Java are shown in the following table:

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

1, and : Require everyone to vote to approve an issue

2, or: Only require one person to vote to approve an issue

3, Non: Someone originally voted to approve, passed Non-operator can invalidate the vote

4. XOR: Only one person can vote to approve an issue

When using logical operators, we will encounter A very interesting "short circuit" phenomenon occurs.

For example: ( one > two ) && ( one < three ) , if it can be determined that the running result of one > two on the left is false, then the system will think that it is no longer necessary to execute one < on the right. Three.

Similarly, in ( one > two ) || ( one < three ) , if it can be determined that the running result of the expression on the left is true , the system will also think that it is no longer necessary to perform the right expression One < three on the side are executed!

5. Conditional operator

The conditional operator (?:) is also called the "ternary operator".

Grammar form: Boolean expression? Expression 1: Expression 2

Operation process: If the value of the Boolean expression is true, then return the value of Expression 1, otherwise return the value of Expression 2

For example:

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

Priority of operators in Java

The so-called priority is the order of operations in an expression. The priorities of commonly used operators in Java are as 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 then add"!

PS: There is no need to memorize the priority order of operators. In actual development, parentheses are generally used to assist in priority management. For example:

Analysis: Parentheses have the highest priority, so

1. Execute a 18, the result is 30

2. Execute ( a 18 ) % 4 modulo, the result is 2

3. Execute a * ( ( a 18 ) % 4 ), the result is 24

The above is the detailed content of What are the java operators?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn