-------Android Training, Java training, look forward to communicating with you! ----------
1. The basic composition of the Java language
1) keywords, identifiers, annotations, constants and variables, operators, statements, functions, arrays
2) Keywords: words that are given special meaning by the Java language, the letters in the keywords are lowercase, such as the keywords used to define the data type: class, Byte, int, short, double, float, long, and so on, define the key word of the Process control: if, while, Default, do, break, return, etc.
3) Identifier: Some name that is customized in the program, it consists of 26 letters of the English alphabet and 0 to 9, and _ & symbols, can not use keywords, numbers can not be used at the beginning
4) name specification in Java:
Package Name: Multiple words are composed of letters are lowercase
Class name Interface name: multi-word composition Yes, all words must be capitalized in the first letter
Variable name and function name: When multiple words are composed, the first letter is lowercase, starting with the second, the first letter capitalized
Constant name: All letters are capitalized, and when multiple words are composed, the words are underlined between them.
2. Arithmetic operators
+ +: Self-adding operation,--: self-subtraction operation
For example: a++, said a plus one after the value assigned to a, equivalent to a=a+1;
b=a++, the order of operations is a first assigns the value to B, then carries on the self-addition operation, B=++a, is a first carries on the self-addition operation, then assigns the value to B
3. Assignment operators
Symbols: + =,-=,/=, *=
These symbols are an assignment symbol, meaning that the numbers on both sides of the symbol are assigned to the left after the operation is completed.
For example: b+=3, equivalent to B=b+3,
B+=3 and b=b+3 difference: + = is an operation, and B=b+3 is two operations, when B is the short type, b=b+3, the statement will compile failed, because B will be promoted to the int type, the result is an int type, cannot be assigned to the short type B, and b+= 3 A cast occurs at the time of the operation, converting the result of the b+3 to the short type, so it can be compiled successfully
4. Logical operators
&: With (and), the result is true only if both sides are true
| : or (or), only if both sides are false, the result is false
^: XOR, sign both sides of the same time, the result is false, both sides of the result is not true
&&: The difference between it and & is that when the left side of the symbol is false, it stops the operation
|| : When the left side of the symbol is true, stop the operation
5, bitwise operators
1.<< (shift left) 2.>> (shift right) 3. >>> (unsigned Right shift) 4.& (with operations)
5. | (or operation) 6.^ (XOR operation) 7.~ (inverse code)
Bitwise operations are performed directly on the binary
Example: 3<<2=12, a number left two-bit, the result is its binary form to the left to move two positions, the end of the empty two bits with 0 complement, the result is equivalent (the number of left shift number of the power), the same right shift will be the first two bits vacated position with 0 complement,
Rule: With what complement is determined by the number of the highest bit of the number, is 0 to fill 0, is 1 on 1
Example use:
How to calculate the value of 2*8 efficiently?
Two number multiplication, in fact, they are converted into binary after the multiplication operation, according to the law of the bitwise operation, 2*8=2*2^3, so you can use 2<<3 to replace the 2*8, bit operation is directly to a number of the binary system to shift, do not need to do multiplication, so improve efficiency.
How do I swap the values of two variables without using a third-party variable?
The law of the XOR operation is: one number to another number of different or two times, get the original number, n ^m^ m=n, so the value of two numbers are interchanged: n=n^m;m=n^m (equivalent to m=n ^m^ m); N=n^m (equivalent to n=n^n^m)
6, ternary operator
Form: (conditional expression)? Expression 1: Expression 2, if the condition is true, the result of the operation is the following expression 1, if false, the result is an expression of 2
Example: How can I assign the maximum value in two numbers to a variable?
Example: int x=3,y=5,z; z= (x>y) x:y;
7. Scope and lifetime of variables
Scope of the variable:
The scope starts at the position defined by the variable and ends with the curly brace where the variable is located;
Life cycle:
The variable is alive in memory starting at the defined position;
When a variable reaches its scope, it disappears in memory;
8. Circular statements
There are three kinds: for,while,do...while
For Loop statement
A: Format
for (initialize statement; Judge conditional statement; Control condition statement) {
loop body statement;
}
B: Precautions
A: Judge conditional statement is Boolean type
B: Loop body statement If it is one, you can omit the curly brace
While Loop
initialization statements;
while (judging condition statement) {
loop body statement;
Control conditional statements (may be available);
}
Do...while Cycle
A: Basic Format
do {
loop body statement;
}while (Judgment condition statement);
B: The difference between three cycles
A:do...while Loop performs at least one loop body
The b:for and while must first determine if the condition is true before deciding whether to execute the loop body
3. Precautions for recycling (dead Loop)
A: Must pay attention to modify the control conditions, otherwise prone to die cycle.
B: The simplest form of dead loop
A:while (True) {...}
B:for (;;) {}
Control jump Statements
(1) Break: Meaning of the Interruption
Role:
Jump out of a single layer loop
Jumping out of a multilayer loop requires a mate of the label statement
(2) Continue: Continue
Function: Jump out of the single-layer loop once, you can continue the next time
(3) Return: Back
Role:
Once a return has been encountered, the program will not continue to execute in the future.
This article is from the "Dot Drop" blog, please be sure to keep this source http://arctictern.blog.51cto.com/10120640/1659578
Dark Horse Programmer---java Basics Summary