What are operators and operators?

Source: Internet
Author: User

What are operators and operators?

An operator is a "function" symbol used to notify Java to perform related operations.

Common operators in Java can be divided into the following types:

Ø Arithmetic Operator Ø value assignment operator Ø comparison operator Ø logical operator Ø conditional Operator

Example:

  • Arithmetic Operators

Arithmetic Operators are mainly used for basic arithmetic operations, such as addition, subtraction, multiplication, and division.

Common Arithmetic Operators in Java:

Where,++And--It can appear on either the left side of the operand or on the right side, but the result is different drops.

Example 1:

Running result:

Example 2:

Running result:

Note: auto-increment and auto-increment OperatorsOperation variables onlyCannot be used directly for Operation values or constants! For example, the writing methods such as 5 ++ and 8 -- are all incorrect!

PS: % is used to calculate the remainder, also known as the modulo operator"

Enter the code implementation function in the specified line of the editor.

Row 7th: calculates the sum of the four ages.

Row 8th: calculate the average of four ages

Row 3: obtainAge1AndAge2Difference

Row 3: obtainAge1Age after auto-Reduction

Running result:

  • Value assignment operator

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

Common Value assignment operators supported by Java are shown in the following table:

  

In the editor, write code in the blank space and apply the value assignment operator to implement the following results:

public class HelloWorld{    public static void main(String[] args) {        int one = 10;               int two=20;        int three=0;        three=one+two;        three+=one;        three-=one;        three*=one;        three/=one;        three%=one;        //System.out.println("three=one+two==>"+three);        //System.out.println("three+=one==>"+three);       //System.out.println("three-=one==>"+three);        //System.out.println("three*=one==>"+three);       //System.out.println("three/=one==>"+three);        System.out.println("three%=one==>"+three);    }}

 

  • Comparison Operators

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

The following table lists common comparison operators in Java:

Note:

1.>, <, >=, <= only the operands on both sides of the left and right are numerical values.

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

Example: In the editor7, 8, 9, 10Add the correct comparison operator to the row to achieve the following running result:

  • Logical operators

Logical operators are mainly used for logical operations. Common logical operators in Java are shown in the following table:

 

 

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

1,And: Requires everyone to vote for a certain topic.

2,Or: Only one person is required to vote for a topic.

3,Non: A user who originally agreed to vote can invalidate his vote through non-Operators

4,Exclusive or: A topic can only be approved by one person.

When using logical operators, we encounter an interesting"Short Circuit"Symptom.

For example, in (one> two) & (one <three), if you can determine that the result of one> two on the left is false, then the system considers that there is no need to execute one <three on the right.

Similarly, in (one> two) | (one <three), if you can determine that the running result of the expression on the left is true, then the system will also think that there is no need to execute one <three on the right!

Example: If there are four people voting for A, B, C, and D, it is up to them to decide whether to pass the vote. Now it is time for the logical operator to "show its talents. In the editor7, 8, 9, 10Enter the code in the line to complete the program

Combined with the running results, we believe it can help you better understand the application of logical operators ~~

  • Conditional Operators

Conditional operators (? :) Is also called the "ternary operator ".

Syntax format:Boolean expression? Expression 1: expression 2

Operation Process: if the value of a Boolean expression isTrue, ReturnsExpression 1OtherwiseExpression 2Value

For example:

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

 

Example:4Enter the code in the row and apply the relational operator to implement "judgment: If score is greater than or equal to 60, the output passes, and the output fails"

 

The running result is:Test Score: Pass

  Java operator priority

The so-called priority is the order of operation in the expression. The following table lists the priorities of common operators in Java:

The highest priority is 1, and the lowest priority is 11.. For example, the result of x = 7 + 3*2 is 13 "first multiply and then add!

PS: You do not need to memorize the priority order of the operator. In actual development, it is generally usedParenthesesHelps you manage priorities.

For example:

Analysis: parentheses have the highest priority, so

1. Execute a + 18 and the result is 30.

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

3. Run a * (a + 18) % 4) and the result is 24.

Example: Section5Enter the following code in the line. We believe that, in combination with the running results, you will have a clearer understanding of the operator priority!

Running result:

 

 

 

 

 

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.