Java Basics (operator/select struct statement/loop Structure statement/method/identifier)
One, Java identifiers
A sequence of characters named for a class, interface, method, and so on.
1. Identifier composition
(1) English letter case;
(2) Number (cannot begin with a number);
(3) $ and _.
2. Naming rules
Constants: Each single letter is capitalized, and if it consists of multiple words, each word is separated by an underscore.
Variables, methods: The first word is all lowercase, if it consists of more than one word, then the first word is all lowercase, starting with the second word, with the first letter of each word capitalized with the first letter of the other words lowercase.
Package: All letters are lowercase, multi-level package with domain name anti-write, each class with "." Separated.
Class, interface: The first letter of each word is capitalized, the remaining letters are lowercase.
Second, the variable
1. Classification
(1) Basic data types
Integer type: Byte, short, int (integer type default type), long
Float type: float, double (float type default type)
Character type: Char
Boolean type: Boolean (True, False)
Note: Long type in use should be added after l/l, if the data range does not exceed the range of int, l/l can be omitted; float type when used, you need to add f/f later.
(2) Reference data type
2. Attention to the problem of variable calculation
(1) Try to calculate under the same data type;
(2) The default type is promoted when different types of data are calculated.
All computed results of byte, shout, char three types are defaulted to int.
For example: Byte b1=3,b2=4,b; B=B1+B2; b=3+4;
Which is the compilation failed? Why is it?
Solution: B=B1+B2; compile failed, byte,short,char between each other, once the operation, the default promotion to an int type, both sides of the type mismatch, so failed.
(3) An advanced type converts to a low-level type by forcing a type conversion.
Target data type variable name = (target data type) (variable value);
3. If the variable has been declared unassigned, it must be assigned a value before use.
Three, assignment operators
Basic assignment Operator: =
Extended Assignment operators: + =,
=,/=,%= (the extended assignment operator hides forced type conversions).
For example: short s+=1; equivalent to short s= (s+1);
is not equivalent to s=s+1.
Four, logical operators
1. Basic logical operators
(1) Logic and &: false is false, otherwise ture;
(2) Logical OR |: True is ture, otherwise false;
(3) Logic xor ^: The same is false, the difference is true;
(4) Logical non!: Not true false, not false, true, even number is itself.
2. Extended logical operators
(1) logic double and &&: function with logical single and if the left expression is false, then the right expression is no longer executed and has a short-circuit effect. Logical AND &, the right-hand expression executes regardless of the expression on the left.
(2) logical Double or | |: function with logic single or, if the left expression is true, the right expression is no longer executed, with a short-circuit effect. On the other hand, the expression on the right is executed regardless of the expression on the left or the logical order.
Five or three mesh (yuan) operator
(An expression)? True result: false result;
The first is to determine whether the expression is true or False if it is more than the expression.
Six, arithmetic operators
1. Basic Arithmetic operators
+ 、-、,/(Result rounding),% (remainder)
+: (1) operator; (2) indicates positive number; (3) string connector.
2. Extended Arithmetic operators
+ + 、--
Use alone: either + + or--after the data or the data, it will increase by 1 or minus 1.
Participate in the operation: if + + or--before the data, the first self-increment or decrement, and then participate in the operation, if + + or--after the data, the first operation, then self-increment or self-reduction.
Seven, keyboard input
1. Create keyboard Entry object.
Scanner Scanner = new Scanner (system.in);
2, guide package.
Import Java.util.Scanner;
Java.lang.* Package without guide package, Scanner is Java.util.Scanner, need guide package.
3, receive keyboard input data (in the case of type int).
int number = Sc.nextint ();
3, output data.
Viii. If statement (select structure)
Format one: a judgment that fits a single condition
if (conditional expression) {
Statement
}
Format two: if (conditional expression) {
Statement 1;
}else{
Statement 2;
}
Format three: if (conditional expression 1) {
Statement 1;
}else if (conditional expression 2) {
Statement 2;
}else if (..) {
Statement 3;
}else{
Statement n+1;
}
IX. SELECT structure Switch statement
1. Format:
switch (expression) {
Case value 1: statement 1; break;
Case value 2: statement 2; Break
....
Default: Statement n+1; Break }
2. Precautions:
(1) The case statement can be followed only by constants, not with the variable! The case after the JS is either a variable or an expression.
(2) The default statement can be anywhere in the switch statement and does not affect code execution, if break is best taken in the statement.
(3) It is suggested to bring the break, otherwise it will cause case penetrating phenomenon.
(4) The end condition of the switch statement: The break statement ends, and the default execution ends at the end of the statement.
(5) The case statement is followed by a colon, which represents the comparison with an expression in the switch statement.
(6) The type of the expression following the switch statement can be Byte,short,int,char. After Jdk5, you can follow the enumeration. After Jdk7, the string type can be followed.
X. For statement (Loop statement)
1. Format:
for (initialization statement; Conditional statement; Step statement) {
loop body statement;
}
2. Precautions:
(1) A conditional statement, whether it is a simple expression or a complex expression, is a boolean-type;
(2) The initialization statement, and the conditional table statements are followed by a semicolon.
Xi. while statement (Loop statement)
1. Format:
Basic format: while (conditional expression) {
Loop statement body;
}
Extended format: (better than basic format)
initialization statements;
while (conditional expression) {
loop body statement;
Step statement;
}
2. The difference between a while loop and a For loop:
(1) from the memory point of view, the for loop is superior to the while loop, and after the FOR Loop statement is executed, the variable is reclaimed in time by the GC garbage collection period, while the loop can access the variables in the loop statement.
(2) from the specific requirements, if the number of cycles is clear, apply for loop, if you do not know the number of cycles, you should use the while loop.
12. Do-while statement (Loop statement)
1. Format:
Basic format: do{
Loop Body Statement
}whiel (Judgment condition statement);
Extended format: initialization statement;
do{
loop body statement;
Control body statement; (Step statement)
}while (conditional judgment statement);
2. Difference from for, while statement: The loop body is executed at least once.
3, in the actual development, the order of precedence is: For statement, while statement, Do-while statement.
13. Break, continue, return function and Difference
Break: Indicates an interruption, end. Cannot be used alone, usually in a switch statement or in a loop statement.
Continue: Continue meaning, jump out of the current loop and go straight into a loop
Cannot be used alone, it is used more in circular statements.
Return: Returns the value of the method, bringing back a result.
14. Method (function)
A block of code that completes a specific function.
1. Format:
public static return type method name (formal parameter list) {
function body
}
2, the method format explanation:
(1) public static: method is static, common method.
(2) return type: Depending on the specific requirements, the type returned, that is, the end of the data type.
(3) Method name: The name of the current code block. Naming rules: All lowercase letters, if multiple words, the first word is all lowercase, the second word begins with the first letter of each word capitalized.
(4) Return: Returns the calculated result of the current code to the caller.
3. Call method
(1) called separately. Applies to methods that have no return value type.
(2) Output call. Direct output of the results, write dead, can not operate on the results. cannot be used for methods with no return value.
(3) Assignment invocation. can result in further action. Applies to methods that have a return value type. cannot be used for methods with no return value.
4. Precautions:
(1) Methods and methods are lateral relationships, and methods can no longer be defined in a method.
(2) Define a method in which the formal parameter must have a data type, or the problem will occur.
(3) When calling a method in main function main (), no data type is required in the actual parameter.
(4) There can be no semicolon in the place with curly braces, and there cannot be curly braces where there is a semicolon.
XV, method overloading
The method name is the same, the parameters are different (the number or type is different).
Note: When testing the program, test the error data, the correct data, and the boundary data.
Java Basics (Operator/select struct statement/loop Structure statement/method/identifier)