Functions (Methods) in Java
Functions, also known as methods in Java, are blocks of code that perform a specific task. They help in organizing code, improving reusability, and making programs easier to understand.
Defining a Method
A method in Java is defined with a return type, a name, parameters (optional), and a body.
// Method Definition
public int add(int a, int b) {
return a + b;
}
// Method Call
public class Main {
public static void main(String[] args) {
int result = add(5, 10);
System.out.println("Sum: " + result);
}
}
Method Parameters and Return Types
Methods can take parameters and return values. The return type can be any data type or void
if the method does not return anything.
// Method with no return type (void)
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
// Method with return type
public double calculateArea(double radius) {
return 3.14 * radius * radius;
}
Method Overloading
Method overloading allows multiple methods to have the same name but different parameters. It is used to increase the readability of the program.
class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add two doubles
public double add(double a, double b) {
return a + b;
}
}
Recursion
Recursion is a technique where a method calls itself to solve a problem. It is useful for tasks like calculating factorials or traversing trees.
public class Main {
// Recursive method to calculate factorial
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5: " + result);
}
}
Static vs Non-Static Methods
Static methods belong to the class and can be called without creating an object. Non-static methods belong to an instance of the class and require an object to be called.
class MathUtils {
// Static method
public static int square(int num) {
return num * num;
}
// Non-static method
public int cube(int num) {
return num * num * num;
}
}
public class Main {
public static void main(String[] args) {
// Calling static method
int squared = MathUtils.square(5);
System.out.println("Square: " + squared);
// Calling non-static method
MathUtils utils = new MathUtils();
int cubed = utils.cube(5);
System.out.println("Cube: " + cubed);
}
}
Varargs (Variable-Length Arguments)
Varargs allow a method to accept a variable number of arguments. It is denoted by three dots (...
).
public class Main {
// Method with varargs
public static int sum(int... numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
public static void main(String[] args) {
System.out.println("Sum: " + sum(1, 2, 3)); // 6
System.out.println("Sum: " + sum(1, 2, 3, 4, 5)); // 15
}
}
Pass-by-Value
Java uses pass-by-value for method arguments. This means that a copy of the value is passed to the method, and changes to the parameter do not affect the original value.
public class Main {
public static void modifyValue(int num) {
num = 100;
System.out.println("Inside method: " + num);
}
public static void main(String[] args) {
int value = 50;
modifyValue(value);
System.out.println("Outside method: " + value);
}
}
Next: Advanced Function Concepts