Java syntax basics 1: java syntax Basics
Basic Java code format
All program code in Java must exist in a class. class is defined using the class keyword. Some modifiers can be provided before the class. The format is as follows:
Modifier class Name
{
Program code
}
Note:
1. Java is strictCase Sensitive.
2. A continuous string in a Java program cannot be written in two separate rows.
Java program comments
There are three types of annotations in Java:
1. Single line comment
Add "//" to the comment in the format:
Code;// Comment content
2. Multi-line comment
It starts with a slash and ends with a star plus a slash.
3. document comments
Start with a slash and add two asterisks. end with an asterisk and a slash.
The content annotated with this method is interpreted as a formal document of the program and can be included in a document generated by a tool program such as javadoc to describe the hierarchy of the program and its method.
Supplement:
1. "//" comments can be nested in multi-line comments, but cannot be nested with multi-line comments.
2. Program comments generally account for 20%-50% of the total program code,"Readability first, efficiency second".
Java identifier
The names of packages, classes, methods, parameters, and variables in Java can be composed of uppercase/lowercase letters, numbers, underscores (_), and dollar signs ($) in any order, it cannot start with a number or be a reserved keyword in Java.
Java keywords
1. Declaration for classes and interfaces: class, extends, implements, interface
2. package introduction and package Declaration: import and package
3. Data Types: byte, boolean, char, double, int, long, float, short
4. Optional values of some data types: flase, ture, and null
5. Process Control: break, case, continue, default, do, else, for, if, return, switch, while
6. Exception Handling: catch, finally, throw, throws, and try
7. modifiers: abstract, final, native, private, protected, public, static, synchronilzed, transient, volatitle
8. OPERATOR: instanceof
9. Create an object: new
10. Reference: this, supper
11. Method return type: void
12. Reserved Words: const, goto
Constants in Java
1. Integer constants
Decimal
Hexadecimal -- starting with 0x or 0X
Octal -- starts with 0
Long Integer -- end with L (l)
2. Floating Point constant
Single-precision floating point number-followed by f (F)
Double-precision floating-point number -- add d (D) to the end)
Note:
A. The default type of fractional constants is double. Therefore, f (F) must be added after float constants ).
B. Floating-point constants can be expressed in an exponential form, for example, 5.022e + 23f.
3. boolean Constants
True or false
4. character constants
It is represented by letters, numbers, escape sequences, and special characters, such as 'A' and '\ t.
The characters in Java take up two bytes and are represented by Unicode code. You can also use '\ U' and Unicode code value to represent the corresponding characters, such as' \ u0027 '.
Common escape characters include:
\ T -- accept keyboard input, which is equivalent to pressing the Enter key
\ N -- indicates line feed
\ T -- indicates a Tab, which is equivalent to a Tab key.
\ B -- indicates the backspace key, which is equivalent to the Back Space key.
\ '-- Single quotes
\ "-- Double quotation marks
\ -- Indicates the Backslash "\"
5. string constants
String constants are enclosed in double quotation marks.
6. null Constants
Null Constant indicates that the object reference is null.
Java variable type
Conversion Between Basic Data Types
1. Automatic type conversion (implicit type conversion)
Condition:
A. The two types are compatible with each other.
B. The value range of the target type must be greater than that of the source type.
2. Forced type conversion (display type conversion)
Format: Target type constant = (target type) Value
Note: strings can be connected with other data types using the plus sign "+" to form a new string.
Variable Scope
The code block determines the scope of the variable, and the scope determines the visibility and time of the variable.
Local variable
The variables defined in a code block in a function or function are called local variables.
Local variables must be initialized or assigned a value before performing the value operation.
Function
1. Define the Function Format
Return Value Type Function Name (parameter type form parameter 1, parameter type form parameter 2 ,...)
{
Program code
Return value;
}
2,Function Overloading
Function overloading means that more than one function with the same name can exist in a class at the same time, as long as they have different numbers or types of parameters.
Operators in Java1. Arithmetic Operator a. String + other types = string B. If you want to remainder a negative number, you can ignore the negative divisor, such as 5%-1 = 1. However, if the divisor is a negative number, it is another matter. 2. assignment operator a. in Java, you can connect the assignment statement, for example, x = y = z = 5; B. x + = y is equivalent to x = x + y. 3. comparison operator a. Pay attention to the difference between the comparison operator "=" and the value assignment operator "=", especially when comparing Boolean variables. B. You can write the expression of the comparison operation in the form of "false = variable" to prevent the value assignment operation from being written by mistake. 4. logical operators a. logical operators are used to calculate the expressions of boolean results. The results of these operations are boolean. B. the difference between "&" and "&" is that if the former is used for join, the expressions on both sides of "&" are involved in the calculation under any circumstances. If the latter connection is used, if the left side of "&" is false, the expression on the right side is not calculated. The differences between "|" and "|" are the same as those between "&" and. 5. bitwise operation-shifts data by binary. Applicable types: byte, short, char, int, long bitwise operations include: & bitwise AND | bitwise OR ^ bitwise OR <left shift, shift n to the left is equal to multiply by the Npower of 2> shift right, shift n to the Npower divided by 2> unsigned shifted to the right Note: a. bitwise operators can also be combined with the "=" value assignment operator to generate new value assignment operators, such as: & =, <=. B. Shift does not change the value of the variable itself. For example, a> 1 is meaningless in a single statement. C. the operands lower than the int type will be automatically converted to the int type before the shift; For the int type Integer shift a> B, the system first modulo B for 32, the result is the actual number of shifts. For long integer shifts, the number of shifts is first modeled on 64.
Operator priorityProgramming Skills: 1. Do not write too complex expressions in a line. You can break them into several statements. 2. use parentheses.
Procedure
1. Ordered Structure
Executed in sequence
2. if Condition Statement
A. if...
B. if... else...
Can be abbreviated as: Variable = Boolean expression? Statement 1: Statement 2;
C. if... else...
3. switch selection statement
Switch (expression)
{
Case value 1:
Statement Block 1;
Break;
...
Case value n:
Statement Block n;
Break;
Default:
Statement Block n + 1;
Break;
}
4. while loop statement
While (conditional expression)
{
Execution statement
}
5. do while LOOP statement
Do
{
Execution statement
} While (conditional expression)
6. for Loop statements
For (initialization expression; loop condition expression; Operation expression after loop)
{
Execution statement
}
7. Enhance the for Loop
For (loop variable type loop variable name: object to be traversed)
{
Execution statement
}
8. break and continue statements
A. a non-label break statement will return the control to the next statement of the current innermost loop (while, do, for, switch.
B. The non-label continue statement is used to skip the remaining statement blocks of the current loop and then execute the next loop.
Array
1. array Definition
For example, int [] x = new int [100];
Or int x [] = new int [100];
2. Static array Initialization
For example, int [] aa = new int [] {3, 4, 5 };
3. Multi-Dimensional Data
For example:
Int [] [] xx = new int [3] [];
Xx [0] = new int [3];
Xx [1] = new int [2];
4. array-related functions
A. system. arraycopy (Object src, int srcPos, Object dest, int destPos, int length) copies the array and stores the length elements starting from the source array srcPos from the position of the destPos of the target array.
B. Array. sort () Array in ascending order