Java Naming Convention
- Package name: all letters are lowercase when composed of multiple words
- Class name and interface name: when composed of multiple words, all words are capitalized
- Variable name and method name: when composed of multiple words, the first word is lowercase, and the first letter of each word is uppercase from the second word
- Constant name: all letters are capitalized. When there are multiple words, each word is connected with an underscore
Java data type
Integer: byte(1), short(2), int(4), long(8)
Floating point type: float(4), double(8)
Automatic type promotion: in particular, byte, short and char operate on each other, and the result is int. (also including mutual operation: byte+byte)
Cast type:
char c = 'a';//a:97,A:65 int num = 10; string str = "hello"; System.out.println(c + num + str); //107hello System.out.println(c + str + num); //ahello10
&Similarities and differences with & &?
- In Java & & and & are both logical operators representing and, and both represent the logical transporter and. When the expressions on both sides are true, the whole operation result is true, otherwise it is false.
- &: logic and, & &: short circuit and
&&When the value of the first expression is false, the second expression will not be calculated& Both expressions execute. - &It can be used as a bitwise operator. When the expressions on both sides of & are not of Boolean type, & indicates bitwise operation.
Scanner class: get different types of variables from the keyboard
Specific implementation steps:
- Import package: import Java util. Scanner;
- Instantiation of Scanner: Scanner scan = new Scanner(System.in);
- Call the relevant methods of the Scanner class to obtain the variables of the specified type.
int num = scan.nextInt();//Input int type String str = scan.next();//Get string double num = scan.nextDouble();//Get floating point
- For the acquisition of char type, Scanner does not provide the corresponding method, and can only obtain one string.
Use of one-dimensional array
1. Declaration and initialization of one-dimensional array
int[] ids;//statement //Static initialization: the initialization of the array is performed simultaneously with the assignment of array elements ids = new int[]{1001,1002,1003}; //Dynamic initialization: the initialization of array and the assignment of array elements are carried out respectively String[] ids = new String[5];
Incorrect writing:
int[] arr1 = new int[]; int[5] arr2 = new int[]; int[] arr3 = new int[3]{1,2,3];
2. How to call the element at the specified position of the array?
Through index call, it starts from 0 and ends with array length - 1.
3. How to get the array length?
Array name.length;
4. How to traverse array elements?
for(int i = 0;i < name.length; ++i){ System.out.println(name[i]); }
5. Default initialization value of array element
- Array elements are integers (byte, short, int, long): 0
- Array elements are floating point: 0.0
- Array elements are char type: 0 or '\ u0000' (not ')
- Array elements are of bolean type: false
- Array element is a reference type: null
6. Memory parsing of array
int[] arr = new int[]{1,2,3}; String[] arr1 = new String[4]; arr1[1] = "Zhang San"; arr1[2] = "Li Si";
Use of two-dimensional arrays
1. Declaration and initialization of two-dimensional array
//initiate static int[][] arr1 = new int[][]{{1,2},{3,4}}; //dynamic initialization String[][] arr2 = new String[3][2]; String[][] arr3 = new String[3][];
Incorrect writing:
String[4][3] arr4 = new String[][]; String[][] arr5 = new String[][5];
2. How to call the element at the specified position of the array?
System.out.println(arr1[0][1]);//1 arr3[1] = new String[4]; System.out.println(arr3[1][0]);//null
3. How to get the array length?
System.out.println(arr2.length);//3 System.out.println(arr2[0].length);//2
4. How to traverse two-dimensional array elements?
for(int i = 0;i < arr2.length; ++i){ for(int j = 0;j < arr2[i].length; ++j){ System.out.println(arr2[i][j]+" "); } System.out.println(); }
5. Default initialization value of array element
Regulation: the two-dimensional array is divided into the elements of the outer array and the elements of the inner array
int[][] arr = new int[4][3];
Outer elements: arr[0],arr[1], etc
Inner elements: arr[0][0],arr[1][2], etc
For example:
int[][] arr = new int[4][3]; System.out.println(arr[0]);//Output address value System.out.println(arr[0][0]);//Output 0 System.out.println(arr);//Output address value
double[][] arr = new double[4][]; System.out.println(arr[0]);//null System.out.println(arr[0][0]);//report errors
Summary:
1, For initialization method 1: int[][] arr = new int[4][3];
- Initialization value of outer element: address value
- Initialization value of inner element: the same as that of one-dimensional array
2, For initialization method 1: int[][] arr = new int[4] [];
- Initialization value of outer element: null
- Otherwise, the initialization value of the element cannot be called, and the inner error of the layer cannot be called
Common algorithms involved in arrays
- Assignment of array elements (Yang Hui triangle, number of loops)
- Find the maximum value and average of numerical array
- Copy, reverse and search of array
- Sorting algorithm of array
About array replication
int[] array1,array2; array1 = new int[]{2,3,5,7}; array2 = array1;
The above is not called array copy, but points to the same address. Array2 is equivalent to the shortcut of array1. If you change the element value in array2, the value in array1 will also change.
True replication:
array2 = new int[array1.length] for(int i = 0;i < array2.length;++i){ array2[i] = array1[i] }
Use of Arrays tool class
java.util.Arrays
- Determine whether two arrays are equal
//boolean equals(int[]a,int[]b) int[]arr1 = new int[]{1,2,3,4}; int[]arr2 = new int[]{1,3,2,4}; boolean isEqual = Array.equals(arr1,arr2); System.out.println(isEqual);//false
- Output array information
//String toString(int[] a) System.out.println(Array.toString(arr1));//null
- Fills the specified element into the array
//void fill(int[] a,int val) Array.fill(arr1,10)
- sort
void sort(int[] a) //Sort from small to large void sort(int[] a, int fromIndex, int toIndex)//This form is to sort the array part, that is, to sort the elements of array a with the subscript from fromIndex to toIndex-1. Note: the elements with the subscript toIndex do not participate in the sorting
Common exceptions in array
- Exception of array subscript out of bounds
- Null pointer exception
//Array subscript out of bounds exception int[] arr1 = new int{1,2,3,4,5}; for(int i = 0;i < arr1.length; ++i){ System.out.println(arr1[i]); }
//Null pointer exception //Case 1: int[] arr2 = new int[]{1,2,3}; arr2 = null; System.out.println(arr2[0]); //Case 2: int[][] arr3 = new int[4][]; System.out.println(arr3[0]);//Null, not null pointer System.out.println(arr3[0][0]);//Null pointer //Case 3: String[] arr4 = new String[]{"AA","BB","CC"}; System.out.println(arr4[0].toString()); arr4[0]=null; System.out.println(arr4[0].toString());//Null pointer