Common Java operators (3)

Source: Internet
Author: User

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 divided into the following types:

Ø arithmetic operators

Ø assignment operator

Ø comparison Operators

Ø logical operators

Ø conditional operator

Let's try to look at the use of operators in a simple example!

When calculating the sum of two numbers, you need to use the addition operator. In Java, the addition operator uses "+" to denote

In the editor, line 5th enters:

int sum = a + b;

The result of the operation is: two sum of: 36

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         intA=12;4         intB=24;5         intsum = a +b;6System.out.println ("Sum of two numbers:" +sum);7     }8}

Arithmetic operators in Java:

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

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 a different drop

Example 1:

1 int a = 5; 2 int b = ++a;  // + + appears on  the left 3 System.out.println ("A:" +a); 4

Operation Result:

Example 2:

1 int a = 5; 2 int B = a + +;  // + + appears on  the right 3 System.out.println ("A:" +a); 4

Operation Result:

Must pay attention to Oh! 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!

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

Last chestnut look:

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         intage1=24;//The age of a given four people4         intAge2=18;5         intage3=36;6         intAge4=27;7         intsum = age1+age2+age3+age4;//find the sum of four ages8         DoubleAVG = SUM/4;//find the average of four ages9         intminus = Age1-age2;//finding the difference between Age1 and Age2Ten         intNewAge =--age1;//to find out the age after Age1 self-reduction OneSYSTEM.OUT.PRINTLN ("Total Age:" +sum); ASystem.out.println ("average age:" +avg); -System.out.println ("Age difference:" +minus); -System.out.println ("Age after Self reduction:" +newage); the  -     } -}

The results of the operation are as follows:

Assignment operators in Java:

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.

Common assignment operators supported by Java, as shown in the following table:

Comparison operators in Java:

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, OH:

1, >, <, >=, <= only support the left and right sides of the operand is a numeric type

2, = =,! = The operands on either side of the number can be numeric types, or they can be reference types

Last chestnut look:

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         intA=16;4         Doubleb=9.5;5String str1= "Hello";6String str2= "Imooc";7System.out.println ("A equals B:" + (A = =b));8SYSTEM.OUT.PRINTLN ("A greater than B:" + (A >b));9System.out.println ("A is less than or equal to B:" + (a <=b));TenSystem.out.println ("str1 equals str2:" + (str1 = =str2)); One     } A}

The results of the operation are as follows:

Logical operators in Java:

Logical operators are primarily used for logical operations. 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.

Chestnuts are as follows:

Assuming that a, B, C, D four people vote, they decide whether to vote, it is time for the logical operator "to".

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         BooleanA =true;//a agree4         Booleanb =false;//b against5         Booleanc =false;//C Objection6         BooleanD =true;//D Agree7System.out.println ((a && B) + "failed");8System.out.println ((a | | b) + "pass");9System.out.println ((!a) + "failed");TenSystem.out.println ((c ^ b) + "pass"); One      A     } -}

The results are as follows:

Conditional operators in Java:

The conditional operator (?:) is 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

Operator Precedence in Java:

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: 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

Let's have a chestnut to practice practiced hand! Expression (11+-8)/4 3 results are ()

Answer: 2 Parse: First step: Calculate 3*8, result is 24; second step: Calculate 11+3*8, result is 35; Third Step: Calculation (11 + 3 * 8)/4, result 8; Fourth step: Calculation (11 + 3 * 8)/4 3, result 2
I'm going to calculate it myself .

Common Java operators (3)

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.