Method Fundamentals
What Is a Method
A method represents the smallest executable unit within a program.
When to Use Methods
Extract repetitive code and logically independent functionality into methods.
Benefits of Methods
- Improves code reusability
- Enhances code maintainability
Method Syntax
public static returnType methodName(parameterList) {
// method body
return value;
}
Method Invocation
Direct Call
methodName(arguments);
Assignment Call
dataType variable = methodName(arguments);
Output Call
System.out.println(methodName(arguments));
Arguments vs Parameters
Actual arguments refer to the values passed during method invocation.
Formal parameters refer to the variables defined in the method signature.
When invoking a method, actual arguments and formal parameters must match in order and type, otherwise compilation errors occur.
Method Design Strategy
What does this method need to accomplish? → method body
What inputs are required to complete the task? → parameters
Method Overloading
Method overloading occurs when multiple methods with the same name exist within a single class. These methods perform similar functionality but differ in parameter types or parameter counts.
Key point: Within the same class, methods with identical names but different parameters constitute an overload relationship. Return types are irrelevant.
Parameter differences include:
- Different parameter counts
- Different parameter types
- Different parameter order
Practice Exercises
Array Copying
import java.util.Arrays;
public class ArrayCopyDemo {
public static void main(String[] args) {
int[] source = {10, 20, 30, 40, 50, 60, 70};
int[] result = extractPortion(source, 2, 6);
System.out.println("Copied array: " + Arrays.toString(result));
}
public static int[] extractPortion(int[] data, int startIndex, int endIndex) {
int[] destination = new int[endIndex - startIndex];
int position = 0;
for (int i = startIndex; i < endIndex; i++) {
destination[position] = data[i];
position++;
}
return destination;
}
}
IDEA Tip: Press Ctrl+Alt+M to quick extract code into a new method.
Prime Number Detection
public class PrimeDetector {
public static void main(String[] args) {
int primeCount = 0;
for (int candidate = 101; candidate <= 200; candidate++) {
boolean isPrime = true;
for (int divisor = 2; divisor < candidate; divisor++) {
if (candidate % divisor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(candidate + " is prime");
primeCount++;
}
}
System.out.println("Total primes found: " + primeCount);
}
}
Captcha Generation
import java.util.Random;
public class CaptchaGenerator {
public static void main(String[] args) {
char[] letters = new char[52];
for (int i = 0; i < letters.length; i++) {
if (i < 26) {
letters[i] = (char) ('a' + i);
} else {
letters[i] = (char) ('A' + i - 26);
}
}
System.out.println("Available characters: ");
for (char c : letters) {
System.out.print(c + " ");
}
System.out.println();
Random generator = new Random();
StringBuilder captcha = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = generator.nextInt(letters.length);
char chosen = letters[index];
captcha.append(chosen);
}
int digit = generator.nextInt(10);
captcha.append(digit);
System.out.println("Generated captcha: " + captcha.toString());
}
}