1, Array;
- Copy: system arraycopy(a,1,b,0,4);
int[ ] b = Arrays.copyOf(a,6); - Sorting: arrays sort(arr); // Ascending order (from small to large)
2, Method:
-
- Encapsulate a specific business logic function
- Be as independent as possible and do only one thing in one way
- Method can be called repeatedly
- Reducing code duplication is conducive to code reuse and code maintenance
- Definition method: five elements
-
Modifier return value type method name (parameter list){
Method body ------------ specific business logic function implementation
}
-
-
Call method:
-
No return value: method name (pass parameter with parameter);
-
With return value: data type variable = method name (with parameter passed to parameter);
-
-
return:
-
Return value// 1) End the execution of the method 2) return the result to the caller
---------- used in methods with return values
-
return; //1) End method execution
------------------ used in methods without return value
-
-
Supplement:
- Formal parameter: formal parameter. The parameter when defining a method is a formal parameter
Argument: actual parameter. The parameter when calling the method is an argument
- Formal parameter: formal parameter. The parameter when defining a method is a formal parameter
3, Classes and objects
- What is class? What is an object?
- Real life is composed of many objects. Classes are extracted based on objects
- Object: a single individual / thing that really exists in software.
Class: category / type, representing a class individual. - Class is the model of object, and object is the concrete real column of class.
- Class can contain:
- Properties / characteristics of objects ---------------------- member variables
- Properties / characteristics of objects ---------------------- member variables
- A class can create multiple objects.
- How to create a class? How to create objects? How do I access members?
public class Student { //The Student class is a reference type we make ourselves //Member variable String name; int age; String address; //method void study(){ System.out.println(name+"I'm learning..."); } void sayHi(){ System.out.println("Hello, my name is"+name+",this year"+age+"Years old, live at home"+address); } } public class StudentTest { public static void main(String[] args){ //Create a student object Student zs = new Student(); //Assign values to member variables zs.name = "zhangsan"; zs.age = 25; zs.address = "Langfang, Hebei"; //Method call zs.study(); zs.sayHi(); Student ls = new Student(); ls.name = "lisi"; ls.age = 24; ls.address = "Jiamusi, Heilongjiang"; ls.study(); ls.sayHi(); //1) Created a student object //2) Assign default values to all member variables Student ww = new Student(); ww.study(); ww.sayHi(); } }
- Overload / overloading of methods -- more convenient for users to access
- Occurs in the same class, with the same method name and different parameter lists
- The compiler will automatically bind the method according to its signature at compile time
//Overloaded demo public class OverloadDemo { public static void main(String[] args) { Aoo o = new Aoo(); o.show(); //The compiler automatically binds a method based on its signature o.show("zhangsan"); o.show(25); o.show("zhangsan",25); o.show(25,"zhangsan"); } } class Aoo{ void show(){} void show(String name){} void show(int age){} void show(String name,int age){} void show(int age,String name){} //int show(){ return 1;} // Compilation error, overload is independent of return value type //void show(String address) {} / / compilation error. Overloading is independent of parameter name }
Supplement:
- High quality code: ------------------ future goals, new year
- Good reusability, expansibility, maintainability, portability, robustness, readability and efficiency
- Default rule:
byte,short,int,long,char-------------0 float,double-------------------------0.0 boolean------------------------------false reference type------------------------------null
-
//To access an object, you need to reference zs quote Data type reference type variable points to object Student zs = new Student();
- Method signature: method name + parameter list