Statement block
- Statement blocks (compound statements) are any number of simple Java statements enclosed in curly braces.
- Block determines the scope of the local variable. The program code in the block, as a whole, is to be executed together.
- A block can be nested within another block, but variables with the same name cannot be declared within two nested blocks.
- A statement block can use external variables, but external variables defined in the statement block cannot be used, because the scope of variables defined in the statement block is limited to the statement block.
Code example: defining variables in a statement block
// Test statement block public class TestLan { public static void main(String[] args) { int n; int a; { int k; int n; // Compilation error: variable n cannot be defined repeatedly } // This ends the scope of variable k } }
method
- A method is a piece of code used to complete a specific function, similar to functions in other languages.
- Method is used to define the behavior characteristics and function implementation of related instances of this class. Methods are abstractions of the behavioral characteristics of classes and objects. Methods are similar to procedure oriented functions.
- In process oriented, function is the most basic unit, and the whole program is composed of function calls.
- In object-oriented, the basic unit of the whole program is class, and the method is subordinate to class and object.
Method declaration format:
[Modifier 1 Modifier 2] Return value type method name(Formal parameter list){ Java Statement: }
Method call method:
Object name. Method name (argument list)
Detailed description of the method:
- Formal parameter (written when defining a method): used to receive external incoming data when declaring a method.
- Argument (written when calling a method): the data actually passed to the method when calling the method.
- Return value: the data returned to the calling environment after the method is executed.
- Return value type: the data type of the return value agreed in advance. If there is no return value, it must be specified as void.
Code example: define method, call method
// Define method, call method public class TestMethod { public static void main(String[] args) { // Define method object TestMethod tm = new TestMethod(); // Call normal methods through objects tm.printSxt(); // Call a parameter method with no return value through an object tm.add(30,40,50); // Call parameter method with return value through object: calculate number int c = tm.add1(10,20,30) + 100; System.out.println("Result of calculation:" + c); } // Add void: it means you don't need to return anything when printing. And there is no formal parameter defined when defining the method void printSxt(){ System.out.println("Printed Sxt"); System.out.println("Ah jun No one yet. November 1, 2021."); } // Parameter method with no return value void add(int a,int b,int c){ int sum = a + b + c; System.out.println("Calculation result of calling parameter method:" + sum); } // Parameter method with return value. Define the data type of the return value in front of the parameter method. int add1(int a,int b,int c){ int sum = a + b + c; // Define the return value of the add1 method return sum; // return function: 1. End the operation of the method. 2. return value } }
Little knowledge
- Return: (1) ends the operation of the method. (2) Specify the return value (the return value will not be printed actively).
- Number, data type, and order of the arguments must match list of the formal parameters of the method declaration being invoked.
- When passing parameters in method calls in Java, follow the principle of value passing (the passed values are copies of data) (copies will assign a formal parameter to the corresponding method in the actual parameter value copy given by the calling method).
- The basic type passes the copy value of the data value.
- The reference type passes the copy value referenced by the object, but points to the same object.
Method overload
Overloaded methods are actually completely different methods, but their names are different.
Minefields: method overload
Composition conditions of method overload:
- Different meanings: parameter type, number and order are different.
- Only different return values do not constitute an overload of the method.
For example, only the return value is different
int a(String str)(){} And void a(String str)(){}
- Only the name of the formal parameter is different, which does not constitute an overload of the method.
For example, only the formal parameter names are different
int a(String str)(){} And int a(String s)(){}
Code example: method overloading
// Test method overload public class TestOverload { public static void main(String[] args) { // Overloading different calling methods System.out.println(add(3,5)); // The result is 8 System.out.println(add(3,4,5)); // The result is 12 System.out.println(add(3.05,5));// The result is 8.05 System.out.println(add(3,5.05));// The result is 8.05 // Overloading of some common methods System.out.println(); // 0 parameters System.out.println(1); // The argument is an int System.out.println(2.0); // The parameter is a double (because float requires f/F to declare, it is a double) System.out.println("cc"); // Parameter is a string } // Summation method public static int add(int n1,int n2){ int sum = n1 + n2; return sum; } // The same method name and different number of parameters constitute an overload public static int add(int n1,int n2,int n3){ int sum = n1 + n2 + n3; return sum; } // The same method name and different parameter data types constitute an overload public static double add(double n1,int n2){ double sum = n1 + n2; return sum; } // The same method name and different order of parameter definition types constitute an overload public static double add(int n1,double n2){ double sum = n1 + n2; return sum; } /** * The same method name and different position order of parameters of the same type do not constitute an overload. * For example, int add(int n1,int n2) {} and int add(int n2,int n1) {} * Only the return value is different (that is, only the data type of the return value of the defined method is different), which does not constitute an overload of the method. * For example, int add(int n1,int n2) {} and double add(int n1,int n2) {} */ }
At this point, the knowledge points related to Java methods are over.