I keyword
**Concept: * * words with special meanings in Java are called
Keywords for Java
There are 51 + 2 keywords in Java
51 keywords and 2 reserved words (goto const - > not available now)
Give meaning (may be given in the future)
**Note: * * is composed of lowercase words, and is in the corresponding editing tool
Is highlighted in
II identifier
concept
In the process of writing programs, we can call them identifiers as long as they are named by us
Naming rules
With letters, numbers, underscores Dollar sign $composition
Cannot start with a number
Cannot be named by keyword (can be named by combination)
Strictly case sensitive
Identifiers with the same name are not allowed in the same {}
Naming conventions for identifiers
General specifications:
See the meaning of the name: the identifier naming is associated with the actual function
Hump naming method: if the identifier is composed of multiple words, the initial letters of other words except the first word are capitalized username and password
Class:
HelloWorld shall be capitalized on the premise that the basic specifications are met
III data type
classification
Basic data types: four categories and eight types
Numeric type: integer type (byte short int long)
And float double
Character type: (char)
Boolean: (boolean)
In the process of code writing, int is used by default for integer type writing, and double is used by default for decimal type writing
IV variable and constant
Concept:
The amount that can be changed during program operation is called variable
The amount that cannot be changed during the running of the program is called a constant
Classification of constants
-
string constant
Multiple characters enclosed in double quotation marks (can contain 0, one or more characters), such as "a", "abc", "China", etc
-
integer constant
Integer, for example: - 10, 0, 88, etc
-
Decimal constant
Decimals, such as: - 5.5, 1.0, 88.88, etc
-
character constants
A character enclosed in single quotation marks, such as' a ',' 5 ',' B ',' medium ', etc
-
Boolean Literals
Boolean value, indicating true or false. There are only two values: true and false
-
Null constant
A special value, null, null
V Process control statement
Sequential flow control statement: it is the default execution order of java program. java program runs one by one from top to bottom and left to right
Select a process control statement:
According to grammar
if... else branch
switch... case branch
According to the number of branches
Single branch
Double branch
Multi branch
Vi The difference between a while loop and a do... While loop
1. The execution process is different. while executes the conditional expression first, and do... while executes the loop body first
2. The execution times of the loop body are different. The while loop may not be executed once, and do... While is executed at least once
3. Different usage scenarios
VII The difference between a while loop and a for loop
1. The syntax is different, and different keywords are used to realize the circular function
2. The scope of initialization variables is different (the initialization variables created by the while loop can also be used after the loop ends)
3. Memory usage difference (variables created in the for loop will be recycled after the for loop ends - > initialization and variables in the method body)
4. Different usage scenarios
(1) If you still need to use the initialization variable after the end of the loop, use while
(2) If you recycle for a limited number of times (there is a fixed number of times)
VIII Process jump statement
break
break: end the current whole cycle process and no longer judge whether the conditions for executing the cycle are valid.
for (int i = 1; i <=10; i++) { if(i==5){ break; } System.out.println(i); } System.out.println("Subsequent code");
continue
Continue: jump out of the current loop and enter the next loop instead of terminating the execution of the whole loop. The current loop will continue to execute subsequent code
for (int i = 1; i <=10; i++) { if(i==5){ continue; } System.out.println(i); } System.out.println("Subsequent code");
return
return: end of method level
End the current method (main method) the subsequent code of the current method will not run
Code cannot be written after return
for (int i = 1; i <=10; i++) { if(i==5){ return; } System.out.println(i); } System.out.println("Subsequent code"); /* 1 2 3 4 */
IX Multidimensional array
Concept of multidimensional array:
The container used to store the same data type is called array. There is no multi-dimensional array in java. The so-called multi-dimensional array is actually that the array can store reference types, and it is a reference type. The concept of multi-dimensional array caused by storing arrays in arrays. Generally, we only use two-dimensional arrays because of the complexity of data stored in multiple layers of arrays
Use of multidimensional arrays
① Statement
//statement //How arrays are declared //1. Data type [] array name; int[] [] arr1; //2. Data type array name []; int []arr2[] ;
② Create
//establish //Dynamic creation //Array name = new [length]; arr1=new int[4][2];// //0 0 //0 0 //0 0 //0 0 //The first [] represents the number of data stored in the current array //The second [] represents the number of data stored in each array stored in the current array //It can be understood that the first [] represents the row index and the second [] column index //Static creation //Data type [] array name = {data1, data2} int[] [] arr3={ {1,2,3}, {4,5,6}, {7,8}};
③ Assignment
//assignment //Array name [index] arr1[0][0]=1; //Gets the first data in the current array //Row index and column index
④ Use
//use System.out.println(arr1[0][0]);
Traversal of two-dimensional array
for (int i = 0; i < arr3.length; i++) { int [] arr=arr3[i]; for(int j=0;j<arr.length;j++){ System.out.print(arr[j]+" "); } System.out.println(); }
X practice
①. Find the number of daffodils from 1 to 999
(one digit cube + ten digit cube + hundred digit cube = itself)
public class Test3 { public static void main(String[] args) { //Find the number of daffodils within 1000 //One bit cube + ten bit cube + hundred bit cube = itself //Daffodils count three, so the actual range should be 100 ~ 999 //Cyclic output of each bit of data for(int i=100;i<=999;i++){ //Get each bit of traversal data int unit=i%10;//Bit data int tens=i/10%10;//Ten digit data int hundreds=i/100;//Hundredth data //Judge whether the calculated data is equal to the traversal data if(unit*unit*unit+tens*tens*tens+hundreds*hundreds*hundreds==i){ System.out.println(i); } } } }
② Prime numbers from. 1 to 100 (numbers that can only be divided by 1 and itself)
public class Test4 { public static void main(String[] args) { //Prime numbers from 1 to 100 (numbers that can only be divided by 1 and itself) //Traverse 1 ~ 100 data for(int i=2;i<=100;i++){ //Create a variable to save whether the current variable is a prime number boolean bool=true;//Default is //If the data is 5, it needs to remainder 1 ~ 5. Only 1 and 5 can be 0 //Let the current data take the remainder of 2 ~ current data - 1. If any is 0, change the variable to false for(int j=2;j<=i-1;j++){ if(i%j==0){ bool=false; break; } } //Determine whether to output according to the value of the variable if(bool){ System.out.println(i); } } } }
③ Fibonacci sequence
1 1 2 3 5 8 13 21 34
import java.util.Scanner; public class Test5 { public static void main(String[] args) { //Fibonacci sequence //1 1 2 3 5 8 13 21 //Use the array to create a Fibonacci sequence of specified length, write and store it in the array //Create according to the length entered Scanner sc=new Scanner(System.in); System.out.println("Please enter the length to create the array"); int length = sc.nextInt(); //Creates an array of the specified length int [] fibonacci=new int[length]; //Generate data for arrays //Generate data for each bit for(int i=0;i<fibonacci.length;i++){ if(i==0||i==1){ fibonacci[i]=1; }else{ //Fibonacci data is after the first two data fibonacci[i]=fibonacci[i-1]+fibonacci[i-2]; } } for(int i=0;i<fibonacci.length;i++){ System.out.print(fibonacci[i]+" "); } } }
④ Yanghui triangle
Save Yang Hui triangle with two-dimensional array
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1
//import java.util.Scanner; public class TwoArray2 { public static void main(String[] args) { // Save Yang Hui triangle with two-dimensional array // 1 // 1 1 // 1 2 1 // 1 3 3 1 // 1 4 6 4 1 // Scanner sc = new Scanner(System.in); // int a = sc.nextInt(); // int arr[][]=new int[a][]; int arr[][] = new int[5][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; for (int j = 0; j < arr[i].length; j++) { if (j == 0 || j == arr[i].length - 1) { arr[i][j] = 1; } else { arr[i][j] = arr[i - 1][j] + arr[i][j - 1]; } System.out.print(arr[i][j] + " "); } System.out.println(); } } }