Java Methods Explained: Concepts and Examples
A. Methods: Methods are a set of instructions or code that can be executed repeatedly. They can be called by a program to perform a specific task.
Example code:
public void greetUser(String name) {
System.out.println('Hello ' + name + '!');
}
In this example, greetUser is a method that takes in a String parameter name and prints out a greeting message.
B. Java API and Package/Library methods: Java API and Package/Library methods are pre-defined methods that are provided by Java or other third-party libraries to perform specific tasks.
Example code:
int[] arr = {1, 2, 3, 4, 5};
Arrays.sort(arr); // Java API method to sort an array
In this example, Arrays.sort() is a Java API method that sorts the elements of an array in ascending order.
C. Scope and duration: Scope refers to the accessibility of a variable or method within a program. Duration refers to the lifetime of a variable or method.
Example code:
public class Example {
private int num = 10; // field variable
public void printNum() {
int num = 5; // local variable
System.out.println(num); // prints 5
}
public static void main(String[] args) {
Example ex = new Example();
ex.printNum();
System.out.println(ex.num); // prints 10
}
}
In this example, num is a field variable with a scope that is accessible throughout the class. printNum() method has a local variable num with a scope that is limited to the method. The duration of the local variable is limited to the execution of the method.
D. Local and Field variables: Local variables are variables that are declared inside a method or block of code and have a limited scope. Field variables are variables that are declared at the class level and can be accessed by all methods of that class.
Example code:
public class Example {
private int num = 10; // field variable
public void printNum() {
int num = 5; // local variable
System.out.println(num); // prints 5
}
public static void main(String[] args) {
Example ex = new Example();
ex.printNum();
System.out.println(ex.num); // prints 10
}
}
In this example, num is a field variable that is declared at the class level and can be accessed by all methods of the class. num inside the printNum() method is a local variable that has a limited scope and can only be accessed within the method.
E. Pass-by-value, Pass-by-reference: Pass-by-value is a mechanism in which a copy of the value of a variable is passed to a method. Pass-by-reference is a mechanism in which the reference to a variable is passed to a method.
Example code:
public class Example {
public void increment(int num) {
num++;
System.out.println('Inside method: ' + num); // prints 6
}
public static void main(String[] args) {
Example ex = new Example();
int num = 5;
ex.increment(num);
System.out.println('Outside method: ' + num); // prints 5
}
}
In this example, increment() method takes an integer parameter num and increments it by 1. When increment() method is called with num = 5, a copy of the value of num is passed to the method. Therefore, the value of num outside the method remains unchanged.
F. Recursion: Recursion is a technique in which a method calls itself to solve a problem. It is often used when a problem can be broken down into smaller sub-problems.
Example code:
public class Example {
public int factorial(int num) {
if (num == 0) {
return 1;
}
return num * factorial(num - 1);
}
public static void main(String[] args) {
Example ex = new Example();
int num = 5;
int result = ex.factorial(num);
System.out.println(result); // prints 120
}
}
In this example, factorial() method calculates the factorial of a given number using recursion. The base case is when num is zero, in which case the method returns 1. Otherwise, the method calls itself with num - 1 as the argument until the base case is reached.
G. Overloading: Overloading is a technique in which multiple methods can have the same name but different parameters. Java determines which method to call based on the number and type of arguments passed.
Example code:
public class Example {
public void print(String str) {
System.out.println(str);
}
public void print(int num) {
System.out.println(num);
}
public static void main(String[] args) {
Example ex = new Example();
ex.print('Hello'); // prints 'Hello'
ex.print(5); // prints 5
}
}
In this example, print() method is overloaded with two different parameters - a String and an int. When print() method is called with a String argument, the first implementation is called. When print() method is called with an int argument, the second implementation is called.
原文地址: https://www.cveoy.top/t/topic/mpQH 著作权归作者所有。请勿转载和采集!