The previous section wrote about some precautions for operators. The beginning of this section is still an explanation of some precautions for operators!
In addition to >, >=, <, <=, ==, !=, comparison operators need to pay attention to instanceof: check whether it is an object of the class, for example: "Hello" instanceof String, return the result bit true .
1. Operation characteristics of logical operator
& (AND): as long as one of both sides of the operation is false, the result will definitely be false. Only when both sides are true, the result will be true; Operation characteristics of
| (OR) : As long as one of the two sides of the operation is true, the result will definitely be true. Only if both sides are false, the result will be false;
^ (XOR) operation characteristics: If the results of both sides of the operation are the same, the result will be false, and the results of both sides will be false. Different, the result is true;
! The operational characteristics of (not): judging the other side of things; the operational characteristics of
&& (short-circuit double AND): basically the same as the & operation, except that when the left side of && is false, the right side does not participate in the operation, and both sides of & are Operation;
|| (short-circuit double OR) operation characteristics: basically the same as | operation, except that when the left side of || is true, the right side does not participate in the operation, and both sides of | must be operated;
2. Bit operators
& (AND bit operator): The operation characteristics are the same as the logical operator &, but here it is for binary bit operations, for example: 6&3=2; (binary principle: 110&011=010), any number and bit is 1 The & operation is the original number (the action takes certain significant digits);
| (or operator): The operation characteristics are the same as the & of the logical operator, but here it is for binary bit operations, for example: 6|3=7 ; (Binary principle: 110|011=111);
^ (XOR operator): The operation characteristics are the same as the logical operator ^, except that here it is for binary bit operations, for example: 6^3^3 = 6, Note: When a number is XORed with the same number twice, the result is still the number itself (functioning as an encryption effect)
~ (inversion operator): The operation characteristics are consistent with ! in the logical operator.
<< (left shift operator): The number of bits shifted to the left is actually the number of times the data is multiplied by 2. Can complete power of 2 operations!
>> (right shift operator): The number of bits shifted to the right is actually the power of the data divided by 2. For the vacancies that appear in high positions, use whatever the original high position is to fill the vacancies!
>>> (unsigned right shift operator): When the data is shifted right, the vacancies in the high bits will be filled with 0 no matter what the original high bits are.
Exercise:
1.//The most efficient way to find out what is 2 times 8? Answer: System.out.println(2<<3);
2. Swap the values of two integer variables?
Answer:
When developing, use the form of third-party variables because it is easy to read.
int c ;
c = a;
a = b;
b = c;
Don’t use this method. If the values of the two integers are too large, they will exceed the range of int and will be forced to convert. The data will
change.
a = a + b; //a = 3 + 5;a = 8;
b = a - b; //3+5-5 = 3;b = 3;
a = a - b; // 3+5-3 = 5;a = 5;
Used during interviews.
a = a ^ b; //a = 3 ^ 5;
b = a ^ b; //b = (3^5)^5; b = 3;
a = a ^ b; //a = ( 3^5)^3; a = 5;
The above is the Java basic introductory essay (3) JavaSE version, the content of the essay javase. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!