Basic grammar

1. Data types

Data types are divided into basic data types and reference data types. The basic data types are numeric: integers (byte,short,int,long), floating-point numbers (float,double), and characters (char). Non-numeric: Boolean. The reference data types are class, interface, and array.

The keyword Memory footprint Value range
byte 1 byte – 128 ~ 127
short 4 – 32768 ~ 32767
int 4 – 2 ^ 31 ~ 2 ^ 31-1
long 8 – 2 ^ 63 ~ 2 ^ 63-1
float 4 3.402823 e + ~ 1.401298 e-45; 1.401298 e-45 ~ 3.402823 e
double 8 1.797693 e+308 ~ 4.900000 e-304; E+308 e-304 4.900000 ~ 1.797693
char 2 0 ~ 65535
boolean 1 True, false

E plus 38 means times 10 to the 38th, and E minus 45 means times 10 to the minus 45.

Type conversion: Automatic type conversion, which assigns a value or variable with a small data range to another variable with a large data range. Byte –>short–>int–>long–>float–>double. Char –>int casts a large number or variable to another variable that represents a small number. Format: target data type variable name =(target data type) value or variable Int k=(Int)88.88

2. The variable

Variable definition format: data type variable name = variable value use matters needing attention: the name cannot be repeated; Variable cannot be used without a value assigned; Long variables are defined by adding L to prevent large integers. When a float type is defined, it is followed by an F to prevent type incompatibilities.

3. The operator

Arithmetic operators, assignment operator: + – * / % = + = – = * = / = % = from increasing the decrement operator: + + – relational operators: = =! = = > > < < = operator is the result of the relationship between Boolean type, true or false logical operators: & | ^! Short circuit logical operators: && | | the ternary operator: format: relational expression? Expression 1: Expression 2; Logical Operators vs. Short-circuit Operators: The right-hand side executes the short-circuit and && regardless of whether the left side is true or false. If the left side is true, the right-hand side executes the short-circuit and &&. If left is false, right or does not perform logic |, whether true and false of the left, the right to perform a short circuit or | |, if the left is false, the right to perform; If the left is true, the right is not executed.

4. Choose structure

If statement, If… . Else statement, if… . Else if… . Else statement switch statement: format: switch(expression){case value 1: body 1; break; Case value 2: body 2; break; … . Default: body n+1; break; } byte,short,int,char,JDK5 can be an enumeration, JDK7 can be a String. Case: followed by the value to be compared with the expression. Break: This is used to end a switch statement. Default: If all the cases do not match, the contents of the case will be executed, similar to the else in the if statement.

5. Loop structure

Format of for loop statement: for (initialization statement; Conditional assertion; Conditional control clause){loop body statement; }

for(i=0; i<=10; I++) {System. Out. Println (" HelloWorld "); }

Number of daffodils: is a three-digit number, the cube sum of ones, tens, and hundreds digits is equal to the original number. If a= I %10 if b= I /10%10 if c= I /100%10 aaa+ BBB + CCC = I 10 if c= I /100%10 if aaa+ BBB + CCC = I Conditional control statement; } the do… While statement format: do{body of the loop; }while(conditional judgment statement); The difference between the three types of loops: a for loop and a while loop determines whether the condition is true, and then decides whether to execute the body of the loop. The do… The.while loop executes the body of the loop once, and then determines whether the condition holds and whether to continue executing the body of the loop. The difference between for and while: The increment variable controlled by a conditional control statement, because the structure of the for loop cannot be accessed again after the end of the for loop. The increment variable controlled by a conditional control statement is not part of the syntactic structure of the while loop and can be used after the end of the while loop. Infinite loop format:

for(;;) { }
while(true) { }
do{ }while(true);

6.IDEA content auxiliary keys and shortcuts

Quick Generate statement Quick Generate main() method: PSVM Enter Quick Generate output statement: sout Enter Content Assist key Ctrl+Alt+ Space (content prompt, code completion, etc.) Shortcut key Note: Single line: Select the code, Ctrl+/, and again, cancel. Multiple lines: Select the code, Ctrl+Shift+/, and once again, cancel. Format: Ctrl+Alt+L

An array of 7.

Array is a definition format for storing multiple storage model arrays of the same type of data: Format 1: Data type [] variable name

          int [] arr

Defines an array of type int, array name is arr. Format 2: Data Type Variable Name []

          int arr []

Define a variable of type int. Arr is the name of the variable. Dynamic initialization: Only the length of the array is specified during initialization, and the system assigns an initial value to the array. ArrayType = new ArrayType [array length];

      int [ ] arr  =  new  int [3];

Static initialization: Specifies the initial value of each array element during initialization. The array length is determined by the system. New datatype [] {datatype 1, datatype 2, datatype 3… . };

Int [] arr = new int [] {1,2,3};

Data type [] variable name = {data 1, data 2, data 3… . }

Int [] arr ={1,2,3}

Two common problems with array operations: Index out of bounds: Accessing an index that does not exist in the array; Null pointer exception: Accessing an array that no longer points to data in heap memory

Method 8.

The method is to organize blocks of code with independent functions into a whole, making it a set of code with characteristic functions. Methods must be created before they are used, a process known as method definition. A method is not run directly after it is created. It needs to be used manually before it is executed. This process is called a method call. Public static void method name (){} method call: method name ();

Parameter: A parameter in a method definition that is equivalent to a variable definition format, such as int number. Argument: A function in a method call that is equivalent to using a variable or constant. Public static data type method name (parameter) {return data; } The return value after the method definition must match the data type on the method definition, otherwise the program will report an error. Method calls with a return value: Format 1: Method name (parameter); isEvenNumber(5); Format 2: datatype variable name = method name (parameter); boolean flag = isEvenNumber(5); The return value of a method is usually received using a variable, otherwise the return value is meaningless.

Public static void method name (data type variable name) {… . Public static void method name (data type variable name 1, data type variable name 2…) {… . } method, the parameter data type and variable name must be missing, missing either program will report an error. When a method is defined, multiple parameters are separated by commas (,).

Method caveats: Methods cannot be nested; Void indicates that there is no return value. You can omit a return or write a return separately, with no data after it. When we define a method, we need to be clear about two things: we need to be clear about the type of return value; we need to be clear about whether any data will be returned after the method is done; if not, we need to write void; If so, write the corresponding data type. Specify parameters: mainly to specify the type and number of parameters. When a method is called, a method of type void is called directly. A non-void method is recommended to be called with a variable.

Method overloading: Method overloading refers to the relationship between multiple methods defined in the same class. Multiple methods meet the following conditions: multiple methods in the same class multiple methods have the same method name multiple methods with different parameters, different types or different amounts. Feature: Overloading only corresponds to the definition of the method, not the method call. Overloading only identifies the names and parameters of the methods in the same class, not the return value, that is, you cannot use the return value to determine whether two methods constitute an overload. Method parameter passing (base type) : For parameters of the base data type, the change of formal parameter does not affect the value of the actual parameter. Method parameter passing (reference type) : For parameters of reference type, changes in the formal parameter affect the value of the actual parameter.

9.Debug

Debug: is a program debugging tool for programmers. It can be used to view the execution process of a program, or to track the execution process of a program to Debug the program. A breakpoint is a marker that tells you where to start to see how to add a breakpoint: select the line of code you want to set a breakpoint on, and click the left mouse button after the line. How to run a program with breakpoints: Right click Debug in the code area to execute. Press F7 to execute and stop to delete the breakpoint: select the breakpoint to be deleted and click the left mouse button.