Example code analysis of Java operator
This article mainly introduces "Java operator example code analysis". In daily operation, I believe many people have doubts about Java operator example code analysis. The editor has consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java operator example code analysis". Next, please follow the editor to study!
One: arithmetic operator
1. What are the arithmetic operators
① basic four operators: +-* /%
② incremental assignment operator: + =-= * = / =% =
③ self-increasing / self-subtracting operator + +-
two。 How to use arithmetic operators
Public class TestDemo {public static void main (String [] args) {int aqum8; int bao20; System.out.println (ahumb); / / + System.out.println (bmera); / /-System.out.println (aqqb); / / * System.out.println (bacca); / double cations 8.0; double dudes 17.0 System.out.println (String c); / / 2.125}} public class TestDemo {public static void main (String [] args) {int aplink 10; ajar 10; / equivalent to Atropa 10; amelight 10; / equivalent to ajar 10; aweed 10; / / equivalent to aweed 10; a Universe 2; / / equivalent to athermal 10; a% visa 2 / equivalent to public static void main;}} public class TestDemo {public static void main (String [] args) {int astat20; astatine; + + a; int breadth; int cantilever; System.out.println (a); / / 24 System.out.println (b); / / 22 System.out.println (c); / / 24}}
3. Considerations for arithmetic operators
There is no need to talk about addition and subtraction. First of all, let's talk about two points for attention in division.
A) the int / int result is still int, which needs to be calculated using double, as shown in the above code
B) 0 cannot be used as a divisor, and an error will be reported if 0 is calculated as a divisor
Let's go on to talk about the points for attention in the operation of the surplus:
The remainder symbol can be used to find the module not only for int but also for double.
Double caterpillar Musi 8.0 / double dances17.0 / System.out.println (d% c); / / 1.0
Finally, I would like to talk about two precautions for the self-increment / self-subtraction operator.
A) if you do not take the return value of the expression of the self-increment operation, there is no difference between pre-increment and post-increment.
B) if you take the return value of the expression, the pre-increment is first self-increment and then assignment followed by self-increment.
Two: relational operators
1. What are the relational operators
There are six relational operators: =! = >
< >= b); System.out.println (a = b);}
3. Considerations for relational operators
The expression return value of the relational operator is of type boolean
Three: logical operators (emphasis)
1. What are the logical operators
Logic and & & logic or | | logic is not!
two。 How to use logical operations
Public class TestDemo {public static void main (String [] args) {int axi10; int bread20; System.out.println (a > b & & ab | | ab)); / / true}}
3. Considerations for logical operators
A) for & &, if the value of the left expression is false, then the overall value of the expression must be false, and there is no need to evaluate the right expression
B) for | |, if the value of the left expression is true, then the overall value of the expression must be true, and there is no need to evaluate the right expression
Four: bit operator
1. What are the bit operators?
The smallest unit of operation on data in Java is not bytes, but binary bits
Bitwise operators include bitwise and & bitwise or | bitwise inverse ~ bitwise XOR ^
two。 How to use the bit operator
Bitwise and &: if both binary bits are 1, the result is 1, otherwise the result is 0
Bitwise or |: if both binary bits are 0, the result is 0, otherwise the result is 1
Bitwise reverse ~: if the bit is 0, it is converted to 1; if the bit is 1, it is converted to 0
Bitwise XOR ^: if the binary bits of two numbers are the same, the result is 0, and if different, the result is 1
3. Considerations for bit operator
Five: shift operation (understanding)
1. What are the shift operators
There are three shift operators: >
two。 How to use the shift operator
Move left: the rightmost position is not needed, the leftmost complement symbol bit (positive complement 0, negative complement 1)
Unsigned move to the right >
3. Considerations for shift operator
A) move 1 bit to the left, equivalent to the original number 2, and move N bits to the left, equivalent to the N power of the original number 2
B) move 1 bit to the right, equivalent to the original number / 2, and move N bits to the right, which is equivalent to the N power of the original number / 2
C) since the shift efficiency of the computer is higher than that of multiplication and division, the shift operation can be used when a code happens to multiply and divide 2 to the N power.
D) it makes no sense to move negative digits or shift too many digits
Six: conditional operator
1. What are the benefits of conditional budget
There is only one conditional operator: expression 1? Expression 2: expression 3
two。 How to use conditional operators
Public class TestDemo {public static void main (String [] args) {int astat10; int biz20; int max=a > bachelorette b; / / 20 System.out.println (max);}}
3. Considerations for conditional operator
When the value of expression 1 is true, the value of the whole expression is the value of expression 2; when the value of expression 1 is false, the value of the whole expression is the value of expression 3
Seven: precedence of operators
The operators are sorted in order of priority: self-addition / subtraction operator, arithmetic operator, comparison operator, logical operator, assignment operator. Please refer to the following table for details:
Sequence
Operator
1.
Parentheses, such as () and []
two。
Unary operators, such as -, +, and!
3.
Arithmetic operators, such as *, /,%, +, and-
4.
Relational operators, such as >, > =,