Third, operator
Java basic operators are grouped by function in the following categories:
1. The arithmetic operator (+,-, *,/,%,++,--) Java extends the addition operator so that it can concatenate strings, such as "abc" + "de", and get the string "ABCDE". Unlike C, C + +, the operand can be a floating-point number, such as 37.2%10=7.2, for the modulo operator%. i++ and ++i difference: i++ After using I, the value of I is added 1, so after the completion of i++, the value of the entire expression is I, and I is the value of i+1;++i before using I, the value of I plus 1, so I finished + After +i, the entire expression and the value of I are i+1.
2. The operator of the relational operator (>, <,> =,<=,==,!=) is characterized by the result of the operation, either True or false. In Java, data of any data type (both basic and combined) can be compared by = = or! = To be equal (this differs from C, C + +). The relational operator is used to compare two values, returning a Boolean value of TRUE or FALSE, instead of 1 or 0 in C, C + +.
3. Boolean logical operators (& | ^! && | |)
Logical operator except! are used to concatenate two Boolean expressions.
&: Only both sides are true and the result is true. Otherwise, it is false.
|: If both sides are false the result is false, otherwise it is true
^: xor: And or a little different.
On both sides, the result is false.
It is true that the results are different on both sides.
& and && difference: &: Whatever the left result is, the right side is involved in the operation.
&&: Short circuit with, if the left is false, then the right side is not a parameter and operation.
| and | | Difference: |: Both sides are calculated.
|| : Short circuit or, if the left is true, then the right side does not participate in the operation.
4. The bitwise operator (>>,< <,> >>,&,|,^,~) is used to manipulate bits operators, such as 2*8 = 2<<3;. >>> (unsigned Right shift)
5. Assignment operator (= + = = *=/=%=)
6. The conditional operator (?:)
7. Other (including the component operator, subscript operator [], instance operator Instanc EOF, memory allocation operator new, coercion type conversion operator (type), method call operator (), etc.)
Example: Swap data for two variables. No third-party variables are required.
int a = 3,b = 5;-->b = 3,a = 5;
A = a + B; A = 8;
b = a A; b = 3;
A = a-B; A = 5;
A = a ^ b;//
b = a ^ b;//b = a ^ b ^ b = a
A = a ^ b;//a = a ^ b ^ a = b;
There are 54 types of operators in Java, which can be divided into 14 levels by priority:
Precedence operator meaning correlation usage
----------------------------------------------------------------
1 [] array subscript left array_name[expr]
. Members Select left Object.member
() method parameter left method_name (expr_list)
() instance constructs left Class_name (expr_list)
+ + suffix self-increment left lvalue++
--suffix self-subtraction left lvalue--
2 + + prefix self-increment right ++rvalue
--Prefix self-subtraction right--lvalue
~ Bitwise FETCH ANTI-Rightist ~expr
! Logical non-right!expr
+ One dollar plus right +expr
-one yuan minus right-expr
3 () Cast right (type) expr
New Object Instantiation right new type ()
New Type (expr_list)
New TYPE[EXPR]
4 * Expr by left expr *
/except left expr/expr
% remaining left expr% expr
5 + Left expr + expr
-Minus left expr-expr
+ String Connection left strexpr + strexpr
6 >> signed right shift left expr >> distance
>>> unsigned Right move left expr >>> distance
7 < Less than left expr < expr
<= less than or equal to left expr <= expr
> Greater than left expr > expr
>= greater than or equal to left expr >= expr
instanceof type comparison left ref instanceof RefType
= = equals Left expr = = Expr
! = does not equal to left expr! = Expr
8 & Integer bitwise AND left integralexpr & integralexpr
& Boolean and left Booleanexpr & booleanexpr
9 ^ Integer Bitwise XOR/left integralexpr ^ integralexpr
^ Boolean xor/left booleanexpr ^ booleanexpr
10 | Integer bitwise OR LEFT integralexpr | integralexpr
| Boolean or Left booleanexpr | booleanexpr
&& logic with left booleanexpr && booleanexpr
12 | | Logical OR Left booleanexpr | | booleanexpr
13? : Conditional arithmetic right booleanexpr? expr:expr
14 = Assignment Right lvalue = expr
*= by assignment right lvalue *= expr
/= Lvalue/= Expr In addition to assignment right
%= modulo assignment right lvalue%= expr
+ = Plus Assignment right lvalue + = expr
+ = string Connection Assignment right Lvalue + = expr
-= minus assignment right lvalue-= expr
<<= left Shift assignment right lvalue <<= expr
>>= signed right Shift assignment right lvalue >>= expr
>>>= unsigned Right Shift assignment right lvalue >>>= expr
&= Integer bitwise AND assignment right lvalue &= expr
&= Boolean and assignment right lvalue &= expr
|= Integer bitwise OR assignment right lvalue |= expr
|= Boolean or assignment right lvalue |= expr
^= Integer Bitwise XOR or Assignment right lvalue ^= expr
^= Boolean xor assignment right lvalue ^= expr
Operators have precedence as well as associativity. The association "left" in the table above indicates that the expression starts from the left, and the associativity is "right", which means that the expression starts from the right side of the operation.
Iv. Process Control statements (If, switch, do, while, for, break, continue)
1. When judging the range of data, obtain the Boolean type of the result of the operation, you need to use if, if the number of fixed values, you can use if, or switch. However, it is recommended to use switch for relatively high efficiency.
Switch (variable) {
Case value: The statement to be executed;
...
Default: the statement to execute;
}
How it works: Use the values of the variables in parentheses to compare the values followed by the case, and the statement following the one that has the same value, and then execute the statement after the default, if not the same;
Details: 1): Break can be omitted, if omitted, has been executed until a break is encountered;
2): The variable in parentheses after switch should be one of the byte,char,short,int four types;
3): Default can be written anywhere in the switch structure, if the default statement is placed in the first row, regardless of whether expression matches value in case, the program starts execution from default until the first break occurs.
2. When certain statements need to be executed many times, the loop structure is used.
While and for can be interchanged. The difference is that if you need to define a variable control loop number. It is recommended to use for. Because the For loop is complete, the variable is freed in memory.
3.break: Action on Switch, and loop statement, used to jump out, or to be called end. When a break statement exists separately, the following do not define other statements, because the compilation fails because it is not executed. When a loop is nested, break only jumps out of the current loop. To jump out of a nested outer loop, just give the loop a name, which is called a label.
4.continue: Only for loop structure, continue to recycle. Effect: End this cycle and continue the next cycle. When the statement is present alone, the statement cannot be defined below and cannot be executed
(ext: http://www.cnblogs.com/doit8791/archive/2012/05/27/2520568.html)
Java Basic Syntax Summary 2