Java Method Parameters

Passing data to methods

📥 What are Method Parameters?

Method parameters allow you to pass data into methods, making them flexible and reusable. Parameters act as variables that receive values when the method is called.


// Method with parameter
public static void greetPerson(String name) {
    System.out.println("Hello, " + name + "!");
}
                                    

Output (when called with "Alice"):

Hello, Alice!

Key Parameter Concepts

📋

Declaration

Define parameter type and name

void method(int number) {}
📤

Arguments

Values passed when calling method

method(42);
🔢

Multiple

Methods can have multiple parameters

void method(int a, String b) {}
🎯

Types

Parameters must have specific types

void method(double price) {}

🔹 Single Parameter Example

Methods with one parameter:

public class ParameterExample {
    
    // Method with String parameter
    public static void sayHello(String name) {
        System.out.println("Hello, " + name + "!");
    }
    
    // Method with int parameter
    public static void printNumber(int num) {
        System.out.println("The number is: " + num);
    }
    
    public static void main(String[] args) {
        sayHello("John");
        sayHello("Sarah");
        printNumber(25);
        printNumber(100);
    }
}

Output:

Hello, John!

Hello, Sarah!

The number is: 25

The number is: 100

🔹 Multiple Parameters

Methods can accept multiple parameters:

public class MultipleParameters {
    
    // Method with two parameters
    public static void introduce(String name, int age) {
        System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
    }
    
    // Method with three parameters
    public static void calculateArea(int length, int width, String unit) {
        int area = length * width;
        System.out.println("Area: " + area + " " + unit);
    }
    
    public static void main(String[] args) {
        introduce("Alice", 25);
        introduce("Bob", 30);
        calculateArea(5, 3, "square meters");
    }
}

Output:

Hi, I'm Alice and I'm 25 years old.

Hi, I'm Bob and I'm 30 years old.

Area: 15 square meters

🔹 Different Parameter Types

Parameters can be of different data types:

public class ParameterTypes {
    
    // String parameter
    public static void printMessage(String message) {
        System.out.println("Message: " + message);
    }
    
    // Integer parameter
    public static void printAge(int age) {
        System.out.println("Age: " + age);
    }
    
    // Double parameter
    public static void printPrice(double price) {
        System.out.println("Price: $" + price);
    }
    
    // Boolean parameter
    public static void printStatus(boolean isActive) {
        System.out.println("Active: " + isActive);
    }
    
    public static void main(String[] args) {
        printMessage("Welcome to Java!");
        printAge(22);
        printPrice(19.99);
        printStatus(true);
    }
}

Output:

Message: Welcome to Java!

Age: 22

Price: $19.99

Active: true

🧠 Test Your Knowledge

What do you call the values passed to a method when calling it?