Java basic algorithm

1: The basic operators of Java include

(1) Arithmetic operators: +, -, *, /, %, ++, –
Arithmetic operators are used to perform arithmetic operations of integer and floating-point types. The two arithmetic operators "++" and "–" are called plus 1 and minus 1 operators respectively. These two operators have a prefix form and a suffix form, and they are different. For example, the execution order of i++ and ++i is different, i++ is +1 after i is used, and ++i is +1 before i is used. The situation of i-- and --i is similar here.

Insert picture description here

(2) Relational operators: >, <, >=, <=,,!= The
relational operator is used to compare two values, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (
) And not equal to (!=) 6 kinds. Relational operators are binary operators, that is, each operator has two operands, and the result of the operation is a logical value. Java allows "==" and "!=" two operators to be used for any data type. For example, you can determine whether the values ​​of two numbers are equal, or whether the instances of objects or arrays are equal. When judging the instance, the comparison is whether the reference addresses of the two objects in the memory are equal.
Insert picture description here

(3) Logical operator:! , &&, ||, &,, |
Edit operators include logical AND (&&), logical OR (||) and logical negation (!). The first two are binary operators, and the latter is unary operators. Java provides a "short-circuit" function for logical AND and logical or, that is, when performing operations, first calculate the value of the expression on the left side of the operator. If the value of the entire expression can be obtained using this value, then skip the right side of the operator The calculation of the side expression, otherwise the expression on the right side of the operator is calculated and the value of the entire expression is obtained.
Insert picture description here

(4) Bitwise operators: >>,<<,>>>,&,|,^,~
Java's bitwise operators can be divided into two types: bitwise operations and shift operations.
Bitwise operators are used for binary bits Perform operations, including bitwise negation (~), bitwise AND (&), bitwise OR (|), exclusive OR (^), right shift (>>), left shift (<<), and unsigned right shift (>>>). Bit operators can only operate on integer and character data.

(5) Assignment operators: =, +=, /=
The role of assignment operators is to assign the value of a constant, variable or expression to a variable. Except for "=", everything else is a special assignment operator. Taking "+=" as an example, x += 3 is equivalent to x = x + 3. First, the addition operation x+3 will be performed, and then the operation result will be assigned to the variable x. -=, *=, /=, %= assignment operators can be deduced by analogy.
Insert picture description here

For example, if the unit price of a commodity is reduced from 10.25 yuan by 1.25 yuan, and the quantity purchased by yourself increases from the original two to 10, you can use the compound assignment operator to calculate the total price of the purchased commodity.Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44941105/article/details/115151939