Methods & Parameters
Methods are blocks of reusable code that perform specific tasks. They can accept input parameters and return output values, making your code modular and organized.
40 min•By Priygop Team•Last updated: Feb 2026
Methods in Java
Example
public class MethodsDemo {
// Method with no parameters, no return
static void greet() {
System.out.println("Hello from Java!");
}
// Method with parameters
static void greetPerson(String name) {
System.out.println("Hello, " + name + "!");
}
// Method with return value
static int add(int a, int b) {
return a + b;
}
// Method with multiple parameters
static double calculateBMI(double weight, double height) {
return weight / (height * height);
}
// Method overloading (same name, different parameters)
static int multiply(int a, int b) {
return a * b;
}
static double multiply(double a, double b) {
return a * b;
}
static int multiply(int a, int b, int c) {
return a * b * c;
}
public static void main(String[] args) {
// Calling methods
greet();
greetPerson("Alice");
int sum = add(15, 25);
System.out.println("Sum: " + sum);
double bmi = calculateBMI(70, 1.75);
System.out.println("BMI: " + String.format("%.1f", bmi));
// Method overloading in action
System.out.println("2 x 3 = " + multiply(2, 3));
System.out.println("2.5 x 3.5 = " + multiply(2.5, 3.5));
System.out.println("2 x 3 x 4 = " + multiply(2, 3, 4));
}
}Try It Yourself: Utility Methods
Try It Yourself: Utility MethodsJava
Java Editor
✓ ValidTab = 2 spaces
Java|43 lines|1290 chars|✓ Valid syntax
UTF-8