Java Basics - Java Operators

Java operators can be divided into functions: arithmetic operators, relational operators, logical operators, bit operators, assignment operators and conditional operators

Table of contents

arithmetic operator

 relational operator

Logical Operators

 bitwise operator

assignment operator 

 conditional operator

 Operator precedence


arithmetic operator

Arithmetic operators include the usual addition (+), subtraction (-), multiplication (*), division (/), modulo (%), and arithmetic operations on integer and floating-point data

The modulo operation in many languages ​​can only be used for integer types. Java has extended this to allow modulo operations on floating-point numbers. For example, 3%2 has a result of 1, and 15.2%5 has a result of 0.2. The modulo operation can also be used with negative numbers, and the result has the same sign as the first operand, for example, 5%-3 is 2, and -5%3 is -2.

In addition, there are two kinds of arithmetic operators "++" and "--", which are called plus 1 and minus 1 operators respectively. These two operators have prefix and suffix forms, which 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 is similar for i-- and --i.

 relational operator

Relational operators are 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 (!=). Relational operators are binary operators, that is, each operator takes two operands, and the result of the operation is a logical value. Java allows two operators "==" and "!=" for any data type. For example, you can check whether the values ​​of two numbers are equal, or whether instances of objects or arrays are equal. When judging the instance, the comparison is whether the reference addresses of the two objects in memory are equal.

Logical Operators

Logical operators include logical AND (&&), logical OR (||), and logical NOT (!). The first two are binary operators, and the latter is a unary operator. 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, and if the value of the entire expression can be obtained by using this value, skip the right side of the operator The side expression is evaluated, otherwise the expression on the right side of the operator is evaluated and the value of the entire expression is obtained

 bitwise operator

Bitwise operators are used to operate on binary bits, including bitwise inversion (~), bitwise and (&), bitwise or (|), exclusive or (^), right shift (>>), left shift ( <<) and unsigned right shift (>>>). Bitwise operators can only operate on integer and character data. 

1. Reversal (~)

A data that participates in the operation is "inverted" according to the binary bit.

Operation rules: ~1=0; ~0=1;

That is: to invert a binary number bit by bit, that is, to change 0 to 1 and 1 to 0.

2. Bitwise AND (&)

For the two data involved in the operation, the "AND" operation is carried out according to the binary digits.

Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1; that is, if two bits are "1 at the same time, the result is "1", otherwise it is 0.

For example: 3&5 is 0000 0011 & 0000 0101 = 0000 0001 Therefore, 3 & 5 is worth 1

3. Bitwise OR (|)

For the two objects involved in the operation, the "or" operation is carried out according to the binary bit.

Operation rules: 0 | 0=0; 0 | 1=1; 1 | 0=1; 1 | 1=1;

That is: as long as one of the two objects participating in the operation is 1, its value is 1.

For example: 3|5, that is 0000 0011|0000 0101 = 0000 0111 Therefore, 3|5 is worth 7

 

4. XOR (^)

The two data involved in the operation are subjected to "exclusive OR" operation according to the binary bit.

Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0; 

That is: two objects participating in the operation, if the two corresponding bits are "different" (different values), the result of the bit is 1, otherwise it is 0

5. Shift left (<<)

Operation rules: Move all the numbers to the left by the corresponding number of digits in binary form, remove the high bits (discard), and fill the low bits with zeros. For example: 12345 << 1, the number 12345 is shifted left by 1 bit:

 

 

 After the shift, the decimal value becomes: 24690, which is exactly twice the value of 12345, so some people will use the left shift operator instead of multiplying by 2, but this does not mean that it is true to multiply by 2. Many times, we can do this Use, but you must know that the shift operator can replace the multiplication by 2 operation in many cases, but this does not mean that the two are the same.

Think about it: If any decimal number is shifted to the left by 32 bits, and the right is filled with 32 0s, wouldn’t the decimal be all 0? of course not! ! ! When the int type data is shifted to the left, when the number of digits shifted to the left is greater than or equal to 32 bits, the number of digits will first calculate the remainder, and then shift to the left, that is, if it is really shifted to the left by 32 bits 12345 < When < 32, the number of digits will be calculated first, that is, 12345<<(32%32) is equivalent to 12345<<0, so the value of 12345<<33 is the same as 12345<<1, both are 24690. 

6. Shift right (>>)

Similarly, taking the value of 12345 as an example, 12345 is shifted to the right by 1 bit: 12345>>1

 

 The value obtained after right shifting is 6172 and the value obtained by dividing 12345 of int type data by 2 is the same, so it is sometimes used to replace the division by 2 operation. In addition, for a displacement of more than 32 bits, like the left shift operator, the number of digits will be calculated first to find the remainder.

 

7. Unsigned right shift (>>>)

The unsigned right shift operator is the same as the right shift operator, but the unsigned right shift operator complements 0 when shifting right, while the right shift operator complements the sign bit. The following is the binary representation of -12345:

 

 For students who are not familiar with the source code, inverse code, and complement code, please study by yourself, and I will not give a supplementary explanation here. Here is a reminder that in the right shift operator, 0 is added after the right shift, because the positive number 12345 symbol The bit is 0, if it is 1, it should be complemented by 1.

 

 1. Explanation of original code, inverse code and complement code: a number can be divided into sign bit (0 plus 1 negative) + true value, and the original code is binary written by our normal thinking. Since computers can only do addition, writing negative numbers in simple binary original code will make mistakes, so everyone invented the inverse code (the positive number remains unchanged, the sign bit of the negative number remains unchanged, and the true value is partially reversed); and later because of +0, - 0 dispute, so the inverse code is improved and turned into a complement code (the positive number remains unchanged, the sign bit of the negative number remains unchanged, the true value is partially inverted, and then +1). The 0 in front of the binary can be omitted, so in summary: the negative numbers in the computer are represented by complement code (sign bit 1, truth part inversion + 1). 2. The relationship between bit operators and 2 Bit operators are very similar to multiplying by 2 and dividing by 2 most of the time, and can be replaced, and the efficiency will be much higher, but remember not to confuse the two; many times someone will Confusing the concepts of the two, especially when the data happens to be even numbers such as 2, 4, 6, 8, 100, etc., it looks more similar, but for odd numbers, such as 12345 used in this article, the result after right shifting is 6172, This result is different from dividing by 2 in the mathematical sense, but for int type data, dividing by 2 will round the result, so the result is also 6172, which is even more confusing

assignment operator 

The role of the assignment operator is to assign the value of a constant, variable, or expression to a variable. 

 

Except for "=", others are special assignment operators. 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 and so on. 

 conditional operator

The conditional operator ( ? : ) is also known as the "ternary operator" or "ternary operator".

Syntax form: Boolean expression? expression1: expression2.

Operation process: If the Boolean expression evaluates to true, return the value of expression1, otherwise return the value of expression2.

 Operator precedence

When calculating an expression, if the expression contains multiple operators, the order of precedence of the operators should be carried out from high to low at a time. The order of precedence of the operators is as follows

 

 

Guess you like

Origin blog.csdn.net/zhangjianming2018/article/details/126594677