Comparison Operators & logical operators
> >= = = < <=! = instanceof
& && | | | ^ ! Logical operators are used to concatenate expressions of two Boolean types
X>2 & X<5
True & true = True
False | False = False
^ xor (different or operation) the same is false, the difference is true.
! Non-operational
Bitwise operators
<< >> >>> (unsigned Right shift) & | ^ ~ (anti-code)
6 & 3 = 2//
6 | 3 = 7//
6 ^ 3 ^ 3 = 6
Shift Operations
3 << 2 = 3 * 2 * 2 = 12//left shift equals several powers multiplied by 2
6 >> 1 = 6/2 = 3//The highest bit is what it takes to fill a bit
>>>//The highest bit in the right shift is 0
Fast calculation 2*8 2<<3
Two integer value swaps
1. Temporary variables
2, a=a+b;b=a-b;a=a-b;//if and more than int range, will cast
A=a^b;b=a^b;a=a^b;
Ternary operators
(conditional expression)? Expression 1 (True): Expression 2 (False)
If
1. A branch
2, two branches//equal to the ternary operator, if the IfElse does not have a result can not be simplified into ternary operators
3. Multiple branches
Nearest principle
If nesting
Local code block
Variable initialization scope
Switch statement
switch (expression) {//Expression int short byte char
Case takes value 1:
Statement 1;
Break
...
Default
Statement
Break
}//case unordered default execution; the break of the last case can be omitted; other break omitted;
If and switch differences
If 1, the specific value of judgment, 2, interval judgment; 3. The result of the operation of the expression is a Boolean
Switch 1, the specific value of the judgment, 2, the number of fixed value, all the answers into memory, high efficiency;
While, do and for
while (conditional expression) {
Execute the statement;
}
do{
Execute the statement;
}while (conditional expression);
Accumulation
Counter
For statement
For (initialization expression 1; loop condition expression 2; post-loop action expression 4) {
Execute statement; (Loop body) 3
}
For and while differences
1, can be interchanged
2, the variable of the loop, the scope is different
Infinite loop form
while (true) {}
for (;;) { }
Java Day 03