Method parameter passing mechanism
1. Method parameters
Parameters are called in () after the method name.
2, classification,
If the method has not yet been called, the value of the parameter is undefined. It acts as an identifier for use in the method body with data passed in from outside. (2) Argument Data (constant value, variable, expression) written to () when calling a method
Expressions can be arguments, but not all expressions can be arguments.
An expression that calls a method that does not return a value cannot be an argument, and anything else can.
Expressions:
- Operation expressions: A +b, a
- The method call expression math.random () is also an expression system.out.println (1) is also an expression
Statement:
- An empty statement
- Expression statement
Not all expressions can be used as statements, only assignment expressions, and increment and decrement expressions. Method call expressions can be added; Called statement A = 1; a++; ++a; System.out.println(1); Math.random(); (3) compound statements if. Else,switch.. case,for,while,do… While (4) jump statement break; continue; Return Return value; return ;
3. Who passes values to whom?
The arguments are responsible for passing values to the parameters.
4. What value is passed?
If the argument is a primitive data type, then the value is passed, that is, the argument copies the value to the parameter. If the argument is a reference data type, then the address value is passed, that is, the argument copies the address value to the parameter.
What’s the difference?
When the parameter is a basic data type, the modification of the parameter is independent of the argument. Parameters are reference data types that modify member variables and affect the arguments. Because the argument gives the parameter the first address of the object, the argument and the parameter refer to the same object.
Method calls memory profiling
1. The JVM has a memory called the Java method stack.
Methods are allocated on a method basis in the stack. When a method is called, the JVM creates a separate space on the method stack to store information about the method’s local variables, a process called “pushing.” When a method call ends, the JVM frees the method’s memory on the stack, a process called “out of the stack.” A stack is a data structure that has a feature called fifo or LIFO.
The difference between member variables and local variables
1. Variables: Used to store data, which essentially represent an area of memory
2. Three elements of memory: data type, variable name, and variable value
Memory has memory address, when the number number, for programmers, if the memory address for programming, it is very troublesome, because memory address is not good memory.
Just like in the network, every machine connected to the Internet has an IP address, but the IP address is a number, users are not easy to remember, use the domain name: www.baidu.com with letters/characters/words instead of IP address.
3. The use of variables is divided into two steps:
(1) Declaration
(2) Initialization
4. Where variables are declared
(1) Inside the method: local variables
(2) Outside of a method in a class: member variables
Member variables can be classified into static variables and instance variables, depending on whether they are static or not (instance variables are non-static member variables).
5. The difference between
(1) Different positions of statements
(2) Different locations of values in memory
Local variables: in the stack
Static variables: methods to go
Instance variable: heap
(3) Initialization mode
Local variables: must be initialized manually.
Member variables: Have default values.
(Static variables: initialized together at class initialization)
(Instance variable: new object)
(4) Life Cycle:
Local variables: initialized when a method is called; each call is independent. The method call ends as the method goes off the stack. The life cycle is shorter.
Static variables: shared by all objects when initialized after a class is loaded and ends when the class is unloaded. Unloading of classes generally does not occur. The life cycle is usually very long.
Instance variables: When a new object is created, and the instance variables of each object are independent. When an object becomes garbage, its collection by the GC ends. Medium life cycle.
(5) Scope
Local variables: start at the declaration and end at {} where they belong
Member variables: in this class
(6) modifier
Local variables: The only modifier that can be added is final, which means that they cannot be modified.
Member variables: public static final, etc.
Conclusion:
Java data types are divided into basic data types and reference data types
Data types can be used in:
(1) Declare variables, local variables, and member variables
(2) represents the return value of the method
(3) The type of parameters in the parameter list of the method. In essence, parameters belong to local variables of the method.
Basic data types: There are eight types and many operators can be supported
Reference data types: The supported operators are ‘==’, ‘==’ compares the memory address of the object, where the String String is special and supports one more +, indicating concatenation.
Base data types do not allow the use of ‘.’.
Reference type: can use ‘.’;
The ‘.’ is called a member accessor, and a variable that refers to a data type can ‘.’ to see what members are declared in the type.
Variable parameter
1. What is a variable parameter?
When a method is called, the number of arguments corresponding to a parameter is variable, called a mutable parameter.
2. Why are there mutable parameters?
For example, declare a method that implements the function of finding the sum of any integer.
public static long sum(int. arr){
int sum = 0;
for(int i = 0; i<arr.length; i++){ sum += arr[i]; }return sum;
}
Copy the code
When the number of parameters is uncertain, we can use mutable parameters, which are used as arrays.
3. Pay attention to
(1) A method can have at most one variable parameter
(2) The variable parameter must be the last parameter in the parameter list
(3) When calling a method, the corresponding number of variable parameters can be 0~ N
The number of arguments corresponding to non-mutable parameters must be the same as the number of parameters
(4) In the declaration method, as an array can be treated.
Method overloading
1. What is method overloading?
In a class, declare two or more methods of the same name with different parameter lists that constitute an overload of the method.
Parameter lists vary from data type to number.
Method overloading, regardless of the return value type.
Or put another way, if two methods differ only in return values, they are not overloaded.
2. Why is method overloading needed?
Method: represents a separate reusable function.
Method name: to see the name to know the meaning, can reflect the function of the method.
Sometimes we implement a function in several ways, and each way is suitable for different situations, usually because the data to be passed in is different, we use a method name to represent their function is the same, but the use of different situations.
Such as:
The system.out.println () method uses an overloading of the method, because we can output multiple types of data, including an int, a decimal, a string, and so on. So we need to define a bunch of println methods to fit different data types, but they all do the same thing.
3. If there are two or more overloaded methods, how do I determine which one it calls?
(1) Find the best match first
The number and types of parameters and arguments are exactly the same
(2) Find compatible types or numbers that meet variable parameters
Byte can be automatically upgraded to short, int…
Byte [] cannot be automatically upgraded to short[],int[]….
Byte [] is a reference data type
(3) If (1) (2) cannot be found, the error is reported
(4) If multiple compatible ones are found, an error will be reported. Such as: the add (int… Nums) and add (int a, int… Nums);
add(int… Nums (int[] nums) and add(int[] nums) are not overloads because they are treated as arrays.
Command line arguments (understood)
1. Command line parameters refer to the parameters passed in by the main method and are called command line parameters. (String [] args)
2. How do I pass parameters to main
(1) In idea
Click Run->Run/Debug Configurations… ->Main Class(select Main Class: the Class that contains the Main method, find the Class that you want to pass the Main argument)-> Enter the parameters to pass in Program arguments, separated by Spaces.
(2) In the command line
Open the command line of the Java file,
Javac -d. Source file Java Java package. Primary class Name Parameter 1 Parameter 2 Parameter 3.....Copy the code
recursive
1. What is recursion?
A method calling itself directly or indirectly is called recursion.
2. Why use recursion?
When the method’s functionality is complete, one of the steps completes the same code as the current method, and you can call yourself to complete it.
package com.dyy.exam; Public class Exam4 {public static void main(String[] args) {system.out.println (jieCheng(5)); } public static int jieCheng(int n){ if(n==1){ return 1; } return n*jieCheng(n-1); }}Copy the code
3. Pay attention to
(1) The recursive process must have an exit, satisfying if() does not recurse, otherwise dead recursion will occur and StackOverflowError will occur