Explain operators and usage in Java
In the previous content, you have learned how to define variables and initialize variables. The purpose of defining variables is to manipulate data. The Java language provides us with code symbols specially used to operate these data, which are collectively referred to as "operators".
According to the usage of operators, we can divide them into the following categories:
Arithmetic operator
Assignment Operators
Self increasing and self decreasing operators
Logical operator
Relational operator
Bitwise Operators
Don't worry, they are just symbols that help us deal with operational data. The following is a code example to illustrate the usage of these operators.
1. Arithmetic operator
Arithmetic operators are arithmetic operations for addition, subtraction, multiplication, division and remainder of numerical variables:
Add: + subtract: - multiply: * divide: / remainder:%
Output results:
2. Assignment operator
Like most programming languages, Java uses the '=' operator for assignment operations. This operation will assign the calculation result on the right (called the right value) to the variable on the left. The assignment operators in Java are:
=(num2 = num1) + = (num2 + = num1 equivalent num2 = num2 + num1) - = (num2 - = num1 equivalent num2 = num2 - num1) * = (num2 * = num1 equivalent num2 = num2 * num1) / = (num2 / = num1 equivalent num2 = num2 / num1)% = (num2% = num1 equivalent num2 = num2% num1)
Result output:
3. Self increasing and self decreasing operators
The self increasing and self decreasing operators operate on only one variable, and the value of the variable changes.
Num + + (equivalent num = num + 1) increases by itself, mainly for numerical variables, so that the value of its own variable increases by 1. Num -- (equivalent num = num - 1) decreases by itself, mainly for numerical variables, so that the value of its own variable decreases by 1.
Result output:
4. Logical operators
Logical operator, as the name suggests, is used for logical judgment. The result of the operation is a Boolean value, that is, true or false. Logical uniform operators have
B1 & & B2: if both B1 and B2 are true, B1 & & B2 will return true; otherwise, it will return false
B1 | B2: if both B1 and B2 are false, false will be returned; otherwise, true will be returned.
! B1: the value opposite to B1 will be returned. If B1 is false, true will be returned; If B1 is true, false is returned
Output results:
Logic short circuit:
In Java, logical operators support short circuit operation. Once the value of the whole expression can be expressed clearly, we don't need to calculate the rest of the expression. For example, if we need to judge that an object is not empty and the return value of one of its methods is not empty, we can judge as follows:
If object is empty, the first part of the expression object= Null returns false, regardless of the expression object after the & & operator someFunction() != The result of null is false. The compiler will automatically optimize these operations and will not execute object someFunction() != null。
5. Relational operators
It is used to compare the data size of two variables and return the Boolean value, that is, true or false
Relational operators include:
'= =' and '! =' Applies to all types of values and objects (that is, basic type variables and reference type variables).
'>', '<', '> =' and '< =' do not apply to Boolean values because they only have true or false, and greater than and less than have no practical significance.
Output results:
6. Bitwise operator
The operation object of bit operator is binary "bit", which can be applied to integer type (int), long type (long), short type (short), character type (char), byte type (byte) and other types. During operation, the corresponding bit (0 or 1) will be subject to Boolean algebra operation or move operation.
The description of computational logic is somewhat obscure. We can more clearly understand how bit operators calculate through examples. Suppose if x equals 60; Y equals 13; Then their binary representation and the results of bit operation are as follows:
Note the difference between > > and > > >
Shift right operator > >. If the value of the operation is positive, insert 0 in the high order; If the value is negative, insert 1 in the high position;
The shift right zeroing operator > > >, whether positive or negative, inserts 0 in the high order.
>Bit operation is not understood. Skip first. When actually used, you can study it again.
7. Operator priority
Operator priority determines the grouping of terms in an expression. It affects how an expression is evaluated. Certain operators have higher priority than other operators.
For example, the multiplication operator has a higher priority than the addition operator. The expression 1 + 5 * 6. According to the priority of the operator, the compiler will calculate 5 * 6 first, then 30 + 1, and finally get the result 31.
The priorities of various operators are as follows:
Don't worry about such a complex priority list. In most cases, the expression itself can easily see the priority. For example, the assignment operation must have the lowest priority. When the priority is not clear, we can use parentheses to change the priority in the way we want, so we don't need to remember too much about the operator priority.
8. Other operators
Three operators:
The conditional operator in Java is a ternary operator. Its form is as follows:
If the Boolean expression value is true, the value of the expression is the value of valuewhentrue, otherwise it is the value of valuewhenfalse.
For example, we can calculate the absolute value of X through the following code:
Through the ternary operator, only one statement y = x > = 0? x : -x; Can be completed, more concise.
Type conversion operator:
We use type conversion in many cases. When appropriate, Java will automatically convert the data type to another according to the data type. For example, if we assign an integer value to a float variable, the compiler will convert int to float and assign it to the variable.
However, in many cases, Java cannot judge whether we need type conversion. In this case, we need type conversion operator, which allows us to explicitly perform type conversion, such as:
We can type both variables and constants.
When converting floating-point numbers, we need to pay attention to truncation. If we want to type 10.9: (int) 10.9, its value is not the rounded 11, but 10