第三章: Controlling Program Flow
1、Mathematical operators(数学运算符)
addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result
2、Relational operators(关系运算符)
The relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=).
3、Logical operators(逻辑运算符)
AND (&&), OR (||) and NOT (!) produces a boolean value of true or false based on the logical relationship of its arguments
4、Bitwise operators(位运算符)
The bitwise AND operator (&)、OR operator (|)、EXCLUSIVE OR, or XOR (^)、NOT (~)
5、Shift operators移位运算符
The left-shift operator (<<)、right-shift operator (>>)
the unsigned right shift >>>、left shift <<<
If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an int. If you’re operating on a long, you’ll get a long result. Only the six low-order bits of the right-hand side will be used,
static void printBinaryInt( int i) { // take an int then
for(int j = 31; j >= 0; j--) // print it out in binary format
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
6、Literals(常量)
Hexadecimal (base 16), which works with all the integral data types, is denoted by a leading 0x or 0X followed by 0-9 or a-f either in uppercase or lowercase
char c = 0xffff; // max char hex value
byte b = 0x
7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0x
2f; // Hexadecimal (lowercase)
int i2 = 0X
2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long.
long n1 = 200L; // long suffix L/l大小写无关
long n2 = 200l; // long suffix (but can be confusing)
1.39 e -47f in Java it means 1.39 x 10-47
7、Precedence revisited(重访优先级)
commentary: “Ulcer Addicts Really Like C A lot.”
Operator type | Operators | |
Ulcer | Unary | + - ++-- |
Addicts | Arithmetic (and shift) | * / % + - << >> |
Really | Relational | > < >= <= == != |
Like | Logical (and bitwise) | && || & | ^ |
C | Conditional (ternary) | A > B ? X : Y |
A Lot | Assignment | = (and compound assignment like *=) |
8、The infamous “goto”
Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism: a label.
label1:
outer-iteration {
inner-iteration {
//...
break; // 1
//...
continue; // 2
//...
continue label1; // 3
//...
break label1; // 4
}
}
The same rules hold true for while:
- A plain continue goes to the top of the innermost loop and continues.
- A labeled continue goes to the label and reenters the loop right after that label.
- A break “drops out of the bottom” of the loop.
- A labeled break drops out of the bottom of the end of the loop denoted by the label.
9、switch
it requires a selector that evaluates to an integral value, such as int or char.
asting from a float or double to an integral value always truncates the number.
It turns out that 0.0 is included in the output of Math.random( ). Or, in math lingo, it is [0,1).