Methods and Parameters
In Java, methods are blocks of code that perform specific tasks. They make code reusable, organized, and easy to read. Instead of repeating the same code multiple times, you can write it once in a method and then "call" the method whenever you need to perform that task.
What is a Method?
A method is like a function that can be defined once and used multiple times in a program. You can think of it as a set of instructions to do something specific.
Structure of a Method:
public returnType methodName(parameters) { // Code to perform the task }
- returnType: The type of value the method will return (e.g., int, String, void if it doesn’t return anything).
- methodName: The name of the method. It should be descriptive and follow naming conventions.
- parameters: Optional inputs for the method (more on this below).
Example of a Method
public void greet() { System.out.println("Hello, welcome!"); }
In the above example, the method greet prints a welcome message.
Calling a Method
To use a method, you need to "call" it by writing its name, followed by parentheses. If the method has parameters, provide values inside the parentheses.
greet(); // Calls the greet method and prints "Hello, welcome!"
Methods with Parameters
Parameters allow you to pass information into methods, so the method can perform tasks based on that information.
public void greetUser(String name) { System.out.println("Hello, " + name + "!"); }
In the above example, the greetUser method takes a parameter called name. When calling this method, you can pass a different name each time.
greetUser("Alice"); // Prints "Hello, Alice!"
greetUser("Bob"); // Prints "Hello, Bob!"
Return Type in Methods
Some methods need to return a result after performing their task. The return type specifies the type of value the method will give back (e.g., int, String, boolean). If the method doesn’t return anything, use void as the return type.
public int add(int a, int b) { return a + b; // Returns the sum of a and b } int result = add(5, 10); // result is 15 System.out.println("Sum: " + result); // Prints "Sum: 15"
In the above example, the add method takes two int parameters and returns their sum.
Method Overloading
In Java, you can have multiple methods with the same name but different parameters. This is called method overloading. It allows you to define different ways to perform similar tasks with the same method name.
public void display(String message) { System.out.println("Message: " + message); } public void display(int number) { System.out.println("Number: " + number); } display("Hello"); // Calls the first method display(123); // Calls the second method
In the above example, both methods are named display, but one takes a String parameter, and the other takes an int parameter. Java knows which one to call based on the argument you provide.
Recursion
Recursion is a programming technique where a method calls itself in order to solve a problem. It’s like a loop, but instead of repeating code directly, the method keeps calling itself with a smaller or simpler version of the problem. Each time the method calls itself, it goes deeper until it reaches a stopping point, called the base case.
Let's understand the recursion with the help of Fibonacci example. The Fibonacci sequence is a series where each number is the sum of the two preceding ones. It starts with 0 and 1, so the sequence is: 0, 1, 1, 2, 3, 5, 8, ....
public int fibonacci(int n) { if (n <= 1) { // Base case return n; } else { // Recursive case return fibonacci(n - 1) + fibonacci(n - 2); } }