Java Methods Explained: A Simple Guide with Examples
A. Methods: Methods are blocks of code that perform specific tasks. They can be either pre-defined in the Java API and Package/Library or created by the user. An example of a pre-defined method is 'println' in the 'System' class, used to print messages to the console.
B. User-defined methods: User-defined methods are created by programmers for specific tasks. These methods can be called from anywhere in the program to execute their code. For example, a method can be created to calculate the area of a circle and called whenever needed.
C. Scope and duration: Scope refers to the visibility of a variable or method within the program. Duration refers to its lifetime during program execution. Variables can have local, global, or class-level scope. Duration can be short-term or long-term.
D. Local and Field variables: Local variables are declared inside a method and can only be accessed within that method. Field variables are declared outside a method and can be accessed by all methods inside a class.
E. Pass-by-value, Pass-by-reference: When a method is called, argument values are passed by value or by reference. In pass-by-value, a copy of the argument is passed to the method, and changes to the copy do not affect the original variable. In pass-by-reference, a reference to the original variable is passed, and changes inside the method will affect the original variable.
F. Recursion: Recursion is a technique where a method calls itself repeatedly until a specific condition is met. This is useful for solving problems that can be broken down into smaller, similar problems, like calculating the factorial of a number.
G. Overloading: Overloading occurs when two or more methods have the same name but different parameters. This allows the programmer to use the same method name for different input types without creating separate methods. For example, an 'add' method can accept both integers and doubles as input.
Example code:
A. Pre-defined method: System.out.println('Hello World');
B. User-defined method: public double calculateArea(double radius){ double area = Math.PI * radius * radius; return area; }
C. Scope and duration: public class Example { private int globalVariable;
public void method1(){
int localVariable = 10;
globalVariable = 20;
}
public void method2(){
// localVariable cannot be accessed here
globalVariable = 30;
}
}
D. Local and Field variables: public class Example { private int fieldVariable;
public void method(){
int localVariable = 10;
fieldVariable = 20;
}
}
E. Pass-by-value, Pass-by-reference: public class Example { public void passByValue(int num){ num = num + 1; }
public void passByReference(int[] arr){
arr[0] = arr[0] + 1;
}
}
F. Recursion: public class Example { public int factorial(int num){ if(num == 0){ return 1; } else { return num * factorial(num - 1); } } }
G. Overloading: public class Example { public void add(int num1, int num2){ int sum = num1 + num2; System.out.println(sum); }
public void add(double num1, double num2){
double sum = num1 + num2;
System.out.println(sum);
}
}
原文地址: https://www.cveoy.top/t/topic/mpQo 著作权归作者所有。请勿转载和采集!