Short-circuiting
Used in logical operation that the expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined. As a result, the latter parts of a logical expression might not be evaluated.
即逻辑判断时,从左往右,在这个过程中一旦结果被确定,停止以后的判断,直接返回结果。
Bitwise Operators
- & (AND operator): 1 & 1 = 1, 1 & 0 = 0
- | (OR operator): 1 | 0 = 1, 0 | 0 = 0
- ^ (XOR operator): 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1
- ~ (NOT operator): ~1 = 0, ~0 = 1
Shift Operators
- << (left-shift operator): shift the operand to left by the number of specified bits, inserting 0s at the lower-order.
- >> (signed right-shift operator): shift the operand to right by the number of specified bits, inserting 0s (positive value) or 1s (negative value) at the upper-order.
- >>> (unsigned right-shift operator): same as ">>" except only use 0 extension.
Promotion
If you perform any mathematical or bitwise operations on primitive data types that are smaller than an int (that is char byte or short), those values will be promoted to int before performing the operations, the the resulting value will be of type int. In such case, casting is a must if you want to assign back into the smaller type.
In general, the largest data type in an expression is the one that determines the size of the result of that expression. (double = float + double)
Casting
- Widening conversion: cast from a data type that hold less information to one that hold much more (int -> float)
- Narrowing conversion: cast from a data type that can hold more information to one that doesn't hold as much (float ->)
With widening conversion an explicit cast is not needed, while for the narrowing conversion the explicit cast is a must.
Java allows you cast any primitive type to any other primitive type, except for boolean.
Java Has No "sizeof"
In C and C++, the sizeof() operator tells you the number of bytes allocated for data items. This is for portability, since different data types might be different sizes on different machines. Thus portability is a huge headache for C and C++.
While in Java, because all the data types are the same size on all machines, so you don't have to think about portability on this level.