Document directory
Thinking in Java [Java programming mechanism] learning notes -- operator operator1. the first operator to be explained is "=" NOTE 1:
If = is used between two references, two references are compared, regardless of whether the value of the referenced object is the same. Therefore, when comparing integer and string values,Strongly recommendedTo use equals.
But when can I use = to compare strings? Here is an example reference:
// These two have the same valuenew String("test").equals("test") ==> true // ... but they are not the same objectnew String("test") == "test" ==> false // ... neither are thesenew String("test") == new String("test") ==> false // ... but these are because literals are interned by // the compiler and thus refer to the same object"test" == "test" ==> true // concatenation of string literals happens at compile time resulting in same objects"test" == "te" + "st" ==> true// but .substring() is invoked at runtime, generating distinct objects"test" == "!test".substring(1) ==> false
NOTE 2: double and float size comparison. When comparing double and float, avoid using = and! =. The reason is that floating point numbers have precision, and the precision is the smallest number that double or float can represent. For example, double d1 = 0.1f; double D2 = 0.1; D1 and D2 are not equal because 0.1 cannot be divisible by Double precision. Therefore, we usually use a small floating point number as the precision unit. the shift operator ">>"," <"and" >>> "in the bitwise operator indicates that" 0xffffffff is-1; 0x0000000 is the largest negative number "Left shift: <, signed shift operation
During the Left shift operation, the binary code of the number of operations is shifted to the specified number of digits, and the empty space after the Left shift is supplemented with 0. In Java, the specific operation to move left has no relationship with the sign bit "1" and "0". Whether it is a positive number or a negative number, it will be multiplied by 2 after moving left. therefore, if the smallest negative number is shifted to 1, it will overflow to 0. for exampleint n1 = 0x80000000;int n2 = n1 << 2;n1 : 0x00000000
Right Shift:>, signed shift operation
The right-shift operation shifts the binary code of the number of operations to the specified number of digits. the empty space after the right-shift is supplemented by the symbol bit. That is, if the positive number is supplemented by 0, the negative number is supplemented by 1.int n1 = 0x80000000;int n2 = n1 >> 2;n1 : 0xE0000000
The value of the unsigned shift is only shifted to the right, and the value of ">>>" is not shifted to the left. 0 is added.int n1 = 0x80000000;int n2 = n1 >> 2;n1 : 0x20000000