Java Method Parameters


In Java, parameters are used to pass data into methods. They make methods flexible and reusable by allowing you to work with different input values instead of hardcoding them. Parameters act as variables that receive values when a method is called.

What Are Method Parameters?

Method parameters are variables defined inside a method’s parentheses. These parameters receive values, known as arguments, when the method is called. For example, if you create a method that adds two numbers, the parameters represent those numbers.

Example:

public static void addNumbers(int a, int b) {
    int sum = a + b;
    System.out.println("Sum: " + sum);
}

Here, a and b are parameters. When the method is called, you pass values to these parameters like this:

addNumbers(10, 20);

The output will be:
Sum: 30

Purpose of Using Parameters

Using parameters helps you write reusable and dynamic methods. Instead of creating separate methods for each case, you can write one method that accepts different values as input.

For example, a method to calculate a product price or find an average can work with different numbers just by passing new arguments each time.

Syntax of Parameters in Java

The general syntax of declaring parameters is:

modifier returnType methodName(dataType parameter1, dataType parameter2, ...) {
    // method body
}

Example:

public static void displayDetails(String name, int age) {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}

When calling this method:

displayDetails("Neha", 25);

Output:

Name: Neha
Age: 25

Single Parameter Example

A method can also take only one parameter:

public static void square(int num) {
    System.out.println("Square: " + (num * num));
}

Calling square(6); will print Square: 36.

Multiple Parameters Example

A method can have multiple parameters separated by commas. The number of arguments in the call must match the number of parameters in the method definition.

Example:

public static void studentDetails(String name, int roll, double marks) {
    System.out.println("Name: " + name);
    System.out.println("Roll No: " + roll);
    System.out.println("Marks: " + marks);
}

When calling:

studentDetails("Aarushi", 102, 88.5);

It prints the student’s details using the values passed.

Passing Values to Methods (Arguments)

When you call a method, the values you pass inside the parentheses are called arguments. These arguments are copied into the parameters when the method is executed.

Example:

public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

Calling greet("Tanya"); will print Hello, Tanya!

Call by Value in Java

Java uses call by value, which means that when you pass a variable to a method, the method receives a copy of that variable’s value — not the actual variable itself.

Example:

public static void changeValue(int num) {
    num = num + 10;
    System.out.println("Inside method: " + num);
}

public static void main(String[] args) {
    int x = 5;
    changeValue(x);
    System.out.println("Outside method: " + x);
}

Output:

Inside method: 15
Outside method: 5

This shows that the original value of x remains unchanged.

Passing Objects as Parameters

When you pass an object as a parameter, Java passes the reference of that object. This allows methods to modify the object’s internal data.

Example:

class Student {
    String name;
}

public class Main {
    public static void changeName(Student s) {
        s.name = "Riya";
    }

    public static void main(String[] args) {
        Student obj = new Student();
        obj.name = "Kavya";
        changeName(obj);
        System.out.println(obj.name);
    }
}

Output:
Riya

Here, the name inside the object changes because the method received the reference to the same object.

Using Parameters with Return Values

A method can take parameters and return a result at the same time.

Example:

public static int multiply(int a, int b) {
    return a * b;
}

When you call:

int result = multiply(4, 5);
System.out.println(result);

The output will be 20.

Passing Arrays as Parameters

You can also pass arrays to methods.
Example:

public static void printArray(int[] arr) {
    for (int num : arr) {
        System.out.print(num + " ");
    }
}

When you call:

int[] numbers = {1, 2, 3, 4, 5};
printArray(numbers);

The output will be:

1 2 3 4 5

Order and Type of Parameters

The order and data types of parameters in a method call must match exactly with the method definition.
For example, if the method is declared as:

public static void display(String name, int age)

You must call it in the same order:

display("Sara", 23);

Calling display(23, "Sara"); would result in a compile-time error.

Summary of the Tutorial

Method parameters in Java make your programs flexible and reusable. They allow you to pass different inputs to a single method, avoiding repetition of code. Whether it’s a single value, multiple values, or even an object, parameters help methods work with varying data efficiently. Java always uses call by value, so the original variables remain unchanged unless you pass objects or arrays.


Practice Questions

  1. Write a Java program with a method that accepts a person’s name as a parameter and prints a greeting message.

  2. Create a method that takes two integers as parameters and prints their sum, difference, and product.

  3. Write a program that defines a method to calculate the area of a rectangle using parameters for length and width.

  4. Create a method that accepts a string and an integer (name and age) and displays a message like “Hello [name], you are [age] years old.”

  5. Write a Java program where a method takes an array of integers as a parameter and prints all even numbers.

  6. Create a method that takes two numbers as parameters and returns the greater number using an if…else statement.

  7. Write a Java method that accepts three subject marks as parameters and prints the average marks.

  8. Create a Java program with a method that accepts an integer as a parameter and prints whether it is a prime number or not.

  9. Write a Java method that takes a string as a parameter and counts how many vowels it contains.

  10. Create a program with a method that accepts two double parameters representing the principal and rate, and returns the simple interest for a fixed time of 2 years.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top