Java Basics

Posted by gplevritis on Thu, 27 Jan 2022 09:58:54 +0100

1, Value passing and reference passing
  • pass by value: refers to copying and passing a copy of the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.
  • pass by reference: refers to directly passing the address of the actual parameter to the function when calling the function, so the modification of the parameter in the function will affect the actual parameter.

Note: in JAVA, there is only value passing, not reference passing

2, Method overloading and method rewriting
Distinguishing pointsMethod overloadingMethod rewrite
definitionThe method name is the same, but the parameter list is different (parameter type, number of parameters, parameter order)The method name, parameter list and return value type are the same
Scope of actionUnder the same categoryIn parent-child classes with inheritance relationship (methods modified by static, final and private cannot be overridden)
Access modifier No requirementCannot be lower than the modifier level of a parent method
Return valueNo requirementIt needs to be consistent with the parent method
annotation@overload@override
3, Recursion

Recursion is calling itself, such as calling the A method in the A method.

The recursive result consists of two parts:

  • Recursive header: when to stop calling its own method. If there is no recursive header, it will fall into an endless loop.
  • Recursive body: when to call its own method.

Java uses the stack mechanism. If the recursion depth is too large, it is easy to cause program crash due to memory overflow. Therefore, recursion should be used as little as possible in actual programming.

An example of using recursive programming, factorial:

public static void main(String[] args) {

	int num=factorial(5);//For example, calculate the factorial of 5
    System.out.println("5 The factorial result of is:"+num);

}

public static int factorial(int n){
    if(n==1){//Recursive header
    	return 1;
    }else{//Recursive body
        return n*factorial(n-1);
    }
}

result:

5 The factorial result is: 120
4, Sparse array

Suppose a two-dimensional array is given:

0 2 0 0 0 0
0 0 0 0 0 0
0 0 0 3 1 0
1 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 2

Analysis array: 7 rows and 6 columns, in which there are only 6 significant numbers, and the other numbers are 0; If you create a two-dimensional array with 7 rows and 6 columns, you need to open up more invalid storage space. You can consider compressing this array. The compressed array only stores the significant numbers and their position coordinates in the original array. In this way, the storage space will be greatly saved. The compressed array is a sparse array; The effect of this sparse array is as follows:

7 6 6
0 1 2
2 3 3
2 4 1
3 0 1
5 4 1
6 5 2

Explain this sparse array:

1. The first row represents the description of the original array: the first column"7"Represents that the original array has 7 rows and the second column"6"Represents that the original array has 6 columns and the third column"6"Represents that the original array has 6 significant digits in total;
2. Except the first row, the remaining rows represent the specific coordinates and values of the significant numbers in the original array: for example, the second row"0 1 2"Corresponding to the original array array1[0][1]=2,The rest of the lines are similar to read

The specific codes are as follows:

public static void main(String[] args) {
    //Initialize array
    int[][] array1=new int[7][6];
    array1[0]= new int[]{0, 2, 0, 0, 0, 0};
    array1[1]= new int[]{0, 0, 0, 0, 0, 0};
    array1[2]= new int[]{0, 0, 0, 3, 1, 0};
    array1[3]= new int[]{1, 0, 0, 0, 0, 0};
    array1[4]= new int[]{0, 0, 0, 0, 0, 0};
    array1[5]= new int[]{0, 0, 0, 0, 1, 0};
    array1[6]= new int[]{0, 0, 0, 0, 0, 2};

    System.out.println("*********Original array array1*********");
    for(int i=0;i<array1.length;i++){
        for (int j=0;j<array1[i].length;j++){
            System.out.print(array1[i][j]+"\t");
        }
        System.out.println();
    }

    System.out.println();
    System.out.println("*********Convert to sparse array array2*********");
    int sum=0;//Number of significant digits
    for(int i=0;i<array1.length;i++){
        for (int j=0;j<array1[i].length;j++){
            if(array1[i][j]!=0){
                sum++;
            }
        }
    }
    //Create sparse array
    int[][] array2=new int[sum+1][3];
    //Sparse array first row
    array2[0][0]=array1.length;
    array2[0][1]=array1[0].length;
    array2[0][2]=sum;
    //The rest of the line
    int count=0;
    for(int i=0;i<array1.length;i++){
        for (int j=0;j<array1[i].length;j++){
            if(array1[i][j]!=0){
                count++;
                array2[count][0]=i;
                array2[count][1]=j;
                array2[count][2]=array1[i][j];
            }
        }
    }
    for(int i=0;i<array2.length;i++){
        for (int j=0;j<array2[i].length;j++){
            System.out.print(array2[i][j]+"\t");
        }
        System.out.println();
    }

    System.out.println();
    System.out.println("*********Restore sparse array to original array array3*********");
    int[][] array3=new int[array2[0][0]][array2[0][1]];
    for(int i=1;i<array2.length;i++){
        array3[array2[i][0]][array2[i][1]]=array2[i][2];
    }
    for(int i=0;i<array3.length;i++){
        for (int j=0;j<array3[i].length;j++){
            System.out.print(array3[i][j]+"\t");
        }
        System.out.println();
    }

}

Output result:

*********Original array array1*********
0	2	0	0	0	0	
0	0	0	0	0	0	
0	0	0	3	1	0	
1	0	0	0	0	0	
0	0	0	0	0	0	
0	0	0	0	1	0	
0	0	0	0	0	2	

*********Convert to sparse array array2*********
7	6	6	
0	1	2	
2	3	3	
2	4	1	
3	0	1	
5	4	1	
6	5	2	

*********Restore sparse array to original array array3*********
0	2	0	0	0	0	
0	0	0	0	0	0	
0	0	0	3	1	0	
1	0	0	0	0	0	
0	0	0	0	0	0	
0	0	0	0	1	0	
0	0	0	0	0	2	
5, instanceof keyword and type conversion
  1. Usage of instanceof:
boolean result = obj instanceof Class

Obj represents an object instance, and Class represents a Class or an interface. If obj is an instance object of Class, or its direct or indirect subclass, or the implementation Class of its interface, the result returns true, otherwise false. Special: if obj is null, the result is false.

  1. Type conversion:

    Transition up (auto transition): child objects are converted to parent objects

    Downward transformation (forced transformation): the parent object is converted to a child object

6, Abstract classes and interfaces
Distinguishing pointsabstract classInterface
constructor There can be construction methodsCannot have constructor
Common variableThere can be ordinary variables, static variables and constantsOnly constants (public static final modifier)
Common methodThere can be ordinary methods and abstract methodsOnly abstract methods (public abstract modification)
Method modifierNo requirementCan only be public decoration
Static methodCan haveCan't have
keywordabstract classinterface
realizationExtensions single inheritanceimplements multiple real

Summary: 1 Abstract analogy to ordinary classes, but there are many abstract methods that can be defined, and classes are decorated with abstract keyword, and the rest are indistinguishable; Other classes inherit abstract classes and need to override their abstract methods, unless the class is also an abstract class;

​ 2. The interface is completely composed of abstract methods and constants. Other classes implement the interface through the implements keyword and rewrite its abstract methods; The interface is equivalent to just defining the specification, and the specific implementation is determined by the implementation class.

Topics: Java