Java basics review
1. Circulation
1.1 for cycle
for(int i =1;i<=10;i++){ System.out.println("Hello world"+i); }
- Supplement: the difference between i + + and + + i
package com.daytwo; public class Tarray { public static void main(String[] args) { //The difference between i + + and + + i int k=1; int c=1; int k1=k++; int c1=++c; System.out.println(k1); //1 System.out.println(c1); //2 /* i++ First assign a value in the operation, such as a=i + +, first assign a=i, and then operate i=i+1, so the result is a==1 ++i First operate on the assignment, for example, a = + I, first operate i=i+1, and then assign a=i, so the result is a==2*/ } }
1.2 while loop
- do... while is executed at least once
- while and for loops are executed at least 0 times (conditions are not met)
package com.daytwo; public class Tarray { public static void main(String[] args) { //WHILE loop //standard format //Extended format int i=0; while(i<10){ System.out.println("Hello world"+i); i++; } //do while /*format * do{ * Circulatory body * }while((condition) * */ int kk=1; do{ System.out.println("Hello world"+kk); kk++; }while(kk<=10); } }
-
Note: the variables of the for loop are defined in parentheses and belong to local variables. While the while and do while loops are written outside, they can continue to be used after the loop ends;
-
Suggestion: if the number of times is certain, select the for loop. If the number of times is not certain, select the while or do while loop
1.3 termination statement
- break
package com.daytwo; public class demo1 { public static void main(String[] args) { /*Keyword break, continue*/ for (int i=1;i<=10;i++){ if(i==4){ break;//End the whole cycle }else{ System.out.println("Hello WORLD"+i); } } } }
- continue
package com.daytwo; public class demo1 { public static void main(String[] args) { /*Keyword break, continue*/ for(int i=1;i<=10;i++){ if(i==4){ continue;//Jump out of this cycle }else{ System.out.println("Hello WORLD"+i); } } } }
Add: the cycle that can never stop is called dead cycle;
2. Array
- An array is a reference type of data
- The data in the array must be of the same type
- The length of the array cannot be changed during program operation
2.1 array creation
- dynamic initialization
//grammar int[] array1=new int[300]; double[] array1 =new double[400] String [] array2=new String[300]
- initiate static
package com.daytwo; public class DemoArray { public static void main(String[] args) { //array //An array is a reference type of data // The data in the array must be of the same type // The length of the array cannot be changed during program operation //dynamic initialization int[] array1=new int[300]; //static state //Omit format int [] arry2 ={15,25,35}; //Another static reputation method //standard format // int[] array2=new int[]{5,15,25}; String str =arry2.toString(); System.out.println(str);//[ I@15db9742 Print the address of the object directly; } }
-
If you are not sure that the elements of the array use dynamic initialization, determine to use static initialization;
-
Directly print the array name to get the hash value of the memory address corresponding to the array
be careful:
- The dynamic name empty array is the content of the array. The rules are as follows
- int defaults to 0
- double defaults to 0.0
- The character type defaults to 0 encoded by '\ u0000' uniquecode
- null if reference type
- There is also a default value process during static initialization, but it will be directly overwritten by the value in braces
2.2 assignment of array
array1[0]=1;array1[1]=10; System.out.println(array1[1]);
2.3 array memory changes
- Singular groups and two arrays do not interact
package com.daytwo; public class Demo { public static void main(String[] args){ /*The memory of the two arrays changes and there is no association*/ int[] array1= new int[3]; array1[1]=10; array1[2]=20; System.out.println(array1); System.out.println(array1[0]); System.out.println(array1[1]); System.out.println(array1[2]); System.out.println("======================"); int[] array2 =new int[3]; array2[1]=100; array2[2]=200; System.out.println(array2); System.out.println(array2[0]); System.out.println(array2[1]); System.out.println(array2[2]); } } /***********Output results*************/ /* [I@15db9742 0 10 20 ====================== [I@6d06d69c 0 100 200 */
-
Generate interactive assignment
package com.daytwo; public class Demo2 { public static void main(String[] args) { int[] array1= new int[3]; array1[1]=10; array1[2]=20; System.out.println(array1); System.out.println(array1[0]); System.out.println(array1[1]); System.out.println(array1[2]); System.out.println("======================"); int[] array2 =array1; System.out.println(array2); System.out.println(array2[0]); System.out.println(array2[1]); System.out.println(array2[2]); System.out.println("======================"); array2[1]=100; array2[2]=200; System.out.println(array2); System.out.println(array2[0]); System.out.println(array2[1]); System.out.println(array2[2]); } } /* Output results [I@15db9742 0 10 20 ====================== [I@15db9742 0 10 20 ====================== [I@15db9742 0 100 200 */
2.4 array null pointer exception
- All reference type data can be assigned a null value, but it represents nothing;
package com.daytwo; public class ArrayEx { public static void main(String[] args) { //Array common exception //1. Array out of bounds // int[] arr ={1,2,3}; // System.out.println(arr[0]); //This is out of bounds // System.out.println(arr[3]); //ArrayIndexOutOfBoundsException //Null pointer exception // All reference type data can be assigned a null value, but it represents nothing; int[] arry=null; System.out.println(arry[0]);//NullPointerException /* * The array must be new initialized to use its elements * If only a null value is assigned without new creation, a null pointer exception NullPointerException will occur * */ } }
2.5 traversal of array
-
General traversal
//Traversal of array int[] arrt={1,2,34,4,56,8,7}; for(int i=0;i<arrt.length;i++){ System.out.println(arrt[i]); }
-
foreach traversal
Traversing an array is to get each element of the array. Generally, traversing an array is implemented by using a for loop, but the for loop is not concise enough. Let's briefly introduce the method of traversing an array using a foreach statement. After Java 5, Java provides a more concise loop: foreach loop, which traverses arrays and collections more concisely. When using the foreach loop to traverse the array, there is no need to obtain the length of the array and set, and there is no need to access the array elements according to the index (subscript). The foreach loop automatically traverses each element of the array and set.
// foreach syntax for(type element: array) { System.out.println(element); }
The foreach statement repeats an embedded statement group for each element in an array or collection of objects. The foreach statement is used to iterate through the collection to get the required information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
Therefore, do not assign a value to the loop variable of foreach.
For example:
package com.daytwo; public class Demo3 { public static void main(String[] args) { //Traversal of array int[] arrt={1,2,34,4,56,8,7}; for(int i=0;i<arrt.length;i++){ System.out.println(arrt[i]); } System.out.println("======================="); for(int a:arrt){ //Generally, do not assign a value; // a=0; System.out.println(a); } } }
2.6 array parameters
-
Arrays can be used as arguments to methods
-
When the method is called, the method passes parameters to the parentheses, and what is passed in is actually the address value of the array.
package com.daytwo; public class Demo6 { /* * Arrays can be used as arguments to methods * When the method is called, the method passes parameters to the parentheses, and what is passed in is actually the address value of the array. * */ public static void main(String[] args) { // TODO Auto-generated method stub int[] arry={1,2,3}; teAray(arry);//The argument is the address value of arry; } public static void teAray(int[] arry){ System.out.println("The received parameter is"+arry);//Address value for(int a:arry){ System.out.println(a); } } }
2.7 array return value
package com.daytwo; public class Demo6 { /* * Arrays can be used as arguments to methods * When the method is called, the method passes parameters to the parentheses, and what is passed in is actually the address value of the array. * */ public static void main(String[] args) { // TODO Auto-generated method stub int[] arry={1,2,3}; teAray(arry);//The argument is the address value of arry; System.out.print(arry[0]); int[] res=col(2,3,4); System.out.println("The return value is"+res);//The return value is the array address System.out.println(res[0]); System.out.println(res[1]); } public static void teAray(int[] arry){ arry[0]=100;//Method to modify the value of the array will be modified directly, which is not as scoped as a local variable, because the array is stored in heap memory System.out.println("The received parameter is"+arry);//Address value for(int a:arry){ System.out.println(a); } } /*Array is used as the return value of the method, and what is returned is also the address value of the array*/ public static int[] col(int i,int b,int c){ int sum=i+b+c; int avg=(i+b+c)/3; int[] arr={sum,avg}; //A function or method can only have one return value System.out.println("The address of the internal array of the method is"+arr); return arr; } }
3. Memory
3.1 memory allocation
- 1. The Stack stores local variables in the method. The operation of the method must be run in the Stack.
- Local variable: a parameter of a method, or a variable inside a method {}
- Scope: once the scope is exceeded, it will disappear from the stack memory immediately
- 2. Heap: everything new comes out is in the heap memory
- There is only one address value in heap memory: hexadecimal
- The data in the heap memory has default values. Rules are similar to arrays;
- Supplement: Boolean is false by default
- 3. * * method Area 😗* Store class related information, including method information
- 4. Native Method Stack: operating system related
- 5. The pc Register is CPU related