day05.2_java introductory learning notes

Bubble sort
Outer cycle: i (0~length-1)

The outer layer loops as many times as the length of the array is

Inner cycle: j(0~j-i-1)

scope of execution

Current element: 0~Array length -i-1 (minus I because the last element is not considered for every execution of the outer layer; minus 1 to avoid j+1 causing the array subscript to cross the bounds)
Next element: 1~Array length-i

Compare the current element with the next
If the current element is larger than the next, the two elements exchange positions
The largest element can be moved to the end of the array after the inner loop has been executed

overload

In the same class
Method with the same name

No overload is inconvenient: method callers do not have to consult the documentation for similar functions, find different method names, reduce learning costs, and improve development efficiency when calling methods.
It's convenient to have resets: when calling a series of overloaded methods, it feels like calling the same method. For users, only one method name is needed to cope with different situations.
limit
Source of limitation: Essentially, just let the system distinguish which method we call.

In the same class, if the method names of the two methods are identical, the parameter lists must be different.

Parameter List Differentiation

Either the number of parameters is different or the type of parameters is different.

Method Variable Parameters

In the actual development process, there are some situations where you are not sure how many parameters will be passed in when calling a method. So in order to make method calls flexible, JavaSE5.0 standard adds variable parameter function.
Declarations and Calls

// The addition that can calculate the sum of any number of integers
// Use String... Args to declare variable parameters
public String add(String ... args) {
​
    System.out.println("Sign: Variable parameters");
​
    String sum = "";
​
    // Within the method body, variable parameters are treated as arrays
    for (int i = 0; i < args.length; i++) {
        sum = sum + args[i];
    }
​
    return sum;
}

recursion

Method calls itself. Recursion is intended to allow code in a method to be executed repeatedly and often further based on the results of the previous execution.


Example:

// Calculate the sum between 1 and N: using recursion
public int sumOperationRecursion(int number) {
​
    if (number == 1) {
​
        return 1;
​
    }else{
​
        return number + sumOperationRecursion(number - 1);
​
    }
}

encapsulation

Hide data or code logic from objects. Operations on the data are done inside the class, hiding the details of the implementation from the outside.

Benefits!

Details of the internal code implementation of an object (or component) can be hidden from the outside. Simplify the difficulty of using objects externally. When using an object externally, call the method exposed by the object.
Make the development of the whole system more component and modular, which is more conducive to the implementation: high cohesion, low coupling.

Example (how to hide your own child's age)

public class MarryObject {
​
    // Set the property's permission modifier to private and do not allow direct external access
    private int age;
​
    // Exposed getXxx() method for obtaining data
    public int getAge() {
        return age;
    }
​
    // setXxx() method for setting exposed data
    public void setAge(int ageOutter) {
​
        // Inside the method, modify external data according to internal logic
        if (ageOutter < 20) {
​
            age = 20;
​
        } else if (ageOutter > 60) {
​
            age = 60;
​
        } else {
​
            age = ageOutter;
​
        }
​
    }
​
}

PS: There are only public and default permission modifiers for class es
... public: means this class can be accessed anywhere in the project (how it is actually used in development)
...default: means this class can only be accessed within the same package (actual development will not use this method)

constructor

If we do not explicitly declare a constructor, the system assigns an implicit constructor back to the class.

The role of constructors

**Role 1:**Create objects.

**Role 2:** Initializes the class during object creation. These actions are written in curly brackets of the constructor.
Initialization is placed inside the constructor and is done automatically by the constructor, so the programmer does not have to think about initializing the object after it has been created.
Component

Example

public class Soldier {
​
    private String soldierName;
​
    public Soldier(String soldierName) {
        this.soldierName = soldierName;
    }
​
}

Constructor Overload
Constructors can also have more than one in the same class, are overload relationships, and are consistent with method overloads.

Constructor-related grammar rules

In the Java language, each class has at least one constructor
The modifier of the default constructor matches the modifier of the class it belongs to
Once the constructor is explicitly defined, the system no longer provides a default constructor
A class can create multiple overloaded constructors
The constructor of a parent class cannot be inherited by a child class

Shortcut keys

Wake Up Shortcut Menu
Alt+Insert

Some computers need to hold down the Fn key and then press Insert to take effect

Declare parameterless constructors
When Constructor is selected in the shortcut menu, return and pop up the following window:
Declare all parameter constructors:

Keywords: Java Algorithm

Added by amargharat on Mon, 07 Feb 2022 19:56:34 +0200