Java Method Parameters - Arguments, Types and Complete Examples

⏱️ 9 min read • Beginner Level • Lesson 34

Lesson 34 of 124 of Java Tutorial

Method parameters in Java are variables written inside method parentheses. They allow a method to receive input values and perform operations using those values.

Before learning method parameters, you should understand Java Methods, Java Variables, and Java Data Types. After this lesson, continue with Java Method Overloading.


What are Method Parameters in Java?

A method parameter is a variable declared in the method definition. It receives a value when the method is called.

Simple Meaning: A parameter is like an input box for a method. You pass a value to the method, and the method uses it.
ParameterIdea.java
static void greet(String name) {
    System.out.println("Hello " + name);
}

In the above method, name is a parameter.

Parameter vs Argument in Java

Many beginners use parameter and argument as the same word, but technically they are different.

Term Meaning Example
Parameter Variable written in method definition. String name
Argument Actual value passed while calling the method. "Ayan"
ParameterVsArgument.java
static void greet(String name) { // name is a parameter
    System.out.println("Hello " + name);
}

public static void main(String[] args) {
    greet("Ayan"); // Ayan is an argument
}

Syntax of Method Parameters

Parameters are written inside the parentheses of a method declaration.

ParameterSyntax.java
Copy Download
returnType methodName(dataType parameterName) {
    // method body
}

For multiple parameters:

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

Method with Single Parameter

A method can receive one parameter. In this example, the method receives a name and prints a greeting.

SingleParameterExample.java
Copy Try Download
public class SingleParameterExample {
    static void greet(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {
        greet("Ayan");
    }
}

Output:

Hello Ayan

Method with Multiple Parameters

A method can receive multiple parameters. Each parameter must have its own data type.

MultipleParametersExample.java
Copy Try Download
public class MultipleParametersExample {
    static void showStudent(String name, int age) {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        showStudent("Ayan", 20);
    }
}

Output:

Name: Ayan
Age: 20
Note: While calling a method, arguments must match the number, order, and compatible data types of parameters.

Parameters with Different Data Types

A method can have parameters of different data types such as int, double, String, boolean, and more.

DifferentTypeParameters.java
Copy Try Download
public class DifferentTypeParameters {
    static void showProduct(String productName, double price, boolean available) {
        System.out.println("Product: " + productName);
        System.out.println("Price: " + price);
        System.out.println("Available: " + available);
    }

    public static void main(String[] args) {
        showProduct("Laptop", 55000.50, true);
    }
}

Output:

Product: Laptop
Price: 55000.5
Available: true

Parameters with Return Value

Parameters are often used with return type methods. The method receives input values, processes them, and returns a result.

ReturnWithParameters.java
Copy Try Download
public class ReturnWithParameters {
    static int add(int number1, int number2) {
        return number1 + number2;
    }

    public static void main(String[] args) {
        int result = add(10, 20);
        System.out.println("Sum: " + result);
    }
}

Output:

Sum: 30

Primitive Type Parameters

Primitive values such as int, double, and boolean are passed by value. This means the method receives a copy of the value.

PrimitiveParameterExample.java
Copy Try Download
public class PrimitiveParameterExample {
    static void changeValue(int number) {
        number = 100;
        System.out.println("Inside method: " + number);
    }

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

Output:

Inside method: 100
Outside method: 50
Important: Java is pass-by-value. For primitive parameters, changing the parameter inside the method does not change the original variable.

Reference Type Parameters

For objects, Java still passes the value of the reference. The method receives a copy of the reference, but both references point to the same object. Therefore, object data can be modified through the parameter.

ReferenceParameterExample.java
Copy Try Download
class Student {
    String name;
}

public class ReferenceParameterExample {
    static void changeName(Student student) {
        student.name = "Rahul";
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Ayan";

        changeName(s1);

        System.out.println(s1.name);
    }
}

Output:

Rahul

Array as Method Parameter

Arrays can be passed as method parameters. This is useful when a method needs to process multiple values.

ArrayParameterExample.java
Copy Try Download
public class ArrayParameterExample {
    static int calculateTotal(int[] marks) {
        int total = 0;

        for (int mark : marks) {
            total += mark;
        }

        return total;
    }

    public static void main(String[] args) {
        int[] studentMarks = {80, 75, 90};
        System.out.println("Total: " + calculateTotal(studentMarks));
    }
}

Output:

Total: 245

Object as Method Parameter

You can pass an object as a method argument. This allows the method to access object data.

ObjectParameterExample.java
Copy Try Download
class Employee {
    String name;
    double salary;
}

public class ObjectParameterExample {
    static void printEmployee(Employee employee) {
        System.out.println("Name: " + employee.name);
        System.out.println("Salary: " + employee.salary);
    }

    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.name = "Ayan";
        emp.salary = 45000.50;

        printEmployee(emp);
    }
}

Output:

Name: Ayan
Salary: 45000.5

Variable-Length Arguments in Java

Variable-length arguments, also called varargs, allow a method to accept zero or more values of the same type. Varargs are written using three dots ....

VarargsExample.java
Copy Try Download
public class VarargsExample {
    static int addNumbers(int... numbers) {
        int total = 0;

        for (int number : numbers) {
            total += number;
        }

        return total;
    }

    public static void main(String[] args) {
        System.out.println(addNumbers(10, 20));
        System.out.println(addNumbers(10, 20, 30, 40));
    }
}

Output:

30
100
Important: A method can have only one varargs parameter, and it must be the last parameter in the parameter list.

Scope of Method Parameters

A method parameter can be used only inside the method where it is declared. It cannot be accessed outside that method.

ParameterScopeExample.java
public class ParameterScopeExample {
    static void showMessage(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) {
        showMessage("Hello Java");

        // message cannot be accessed here
    }
}

Complete Example of Method Parameters

The following example uses different parameter types together: primitive values, strings, arrays, objects, return values, and varargs.

MethodParametersCompleteExample.java
Copy Try Download
class Student {
    String name;
    int[] marks;
}

public class MethodParametersCompleteExample {

    static int calculateTotal(int... marks) {
        int total = 0;
        for (int mark : marks) {
            total += mark;
        }
        return total;
    }

    static void printStudentResult(Student student) {
        int total = calculateTotal(student.marks);
        System.out.println("Student: " + student.name);
        System.out.println("Total Marks: " + total);
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.name = "Ayan";
        student.marks = new int[] {80, 75, 90};

        printStudentResult(student);
    }
}

Output:

Student: Ayan
Total Marks: 245

Explanation

  1. Student is an object type used as a method parameter.
  2. marks is an array passed to a method.
  3. calculateTotal() uses varargs to calculate total marks.
  4. printStudentResult() accepts a Student object as parameter.
  5. The program prints student name and total marks.

Common Mistakes with Method Parameters

  • Passing arguments in the wrong order.
  • Passing wrong data types to parameters.
  • Forgetting to specify data type for each parameter.
  • Trying to access a parameter outside its method.
  • Confusing parameter names with argument values.
  • Using more than one varargs parameter in one method.
  • Writing varargs before normal parameters.
Summary:
  • Parameters are variables declared in method parentheses.
  • Arguments are actual values passed while calling a method.
  • A method can have zero, one, or many parameters.
  • Parameters can be primitive types, reference types, arrays, or objects.
  • Java passes parameter values by value.
  • Varargs allow a method to accept variable number of arguments.
  • Method parameters can be used only inside the method where they are declared.

Frequently Asked Questions

Method parameters in Java are variables declared inside method parentheses. They receive values when the method is called.

A parameter is a variable written in the method definition, while an argument is the actual value passed when calling the method.

Yes, a Java method can have multiple parameters. Each parameter must have its own data type and parameters are separated by commas.

Yes, a method can have parameters with different data types such as int, double, String, boolean, arrays, and objects.

No, Java does not directly support default parameters. You cannot assign default values in the method parameter list.

Default parameter behavior in Java is commonly achieved using method overloading. One method calls another method with a default value.

Java passes all method arguments by value. For primitive types, a copy of the value is passed. For objects, a copy of the reference value is passed.

Changing a primitive parameter inside a method does not change the original variable because Java passes a copy of the value.

Yes, if an object is passed as an argument, the method can modify the object's fields because the copied reference still points to the same object.

Yes, arrays can be passed as method parameters. This is useful when a method needs to process multiple values.

Varargs, or variable-length arguments, allow a method to accept zero or more values of the same type using three dots (...).

A Java method can have only one varargs parameter, and it must be the last parameter in the parameter list.

A method parameter can be accessed only inside the method where it is declared. It cannot be used outside that method.

If arguments are passed in the wrong order, the method may produce incorrect results or cause a compilation error if the data types do not match.

Yes, a method parameter can have the same name as a class variable, but it may hide the class variable inside the method. The this keyword can be used to refer to the class variable.

Next step: Learn Java Method Overloading

🚀 Continue to Java Method Overloading →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Method-parameters | Language: Java

Q1. Can an array be passed as a method parameter in Java?
Q2. Which syntax is invalid in Java?
Q3. Can object fields be modified through a method parameter?
Q4. What are varargs in Java?
Q5. What are method parameters in Java?
Q6. Where must the varargs parameter be placed?
Q7. Which is a valid method with one parameter?
Q8. What is an argument in Java method calling?
Q9. What must match when calling a method with parameters?
Q10. How does Java pass primitive method arguments?
Q11. How many varargs parameters can a method have?
Q12. What will be the output of this code?
public class Test {
    static void greet() {
        greet("Guest");
    }

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

    public static void main(String[] args) {
        greet();
    }
}
Q13. What will be the output of this code?
public class Test {
    static void change(int number) {
        number = 100;
    }

    public static void main(String[] args) {
        int value = 50;
        change(value);
        System.out.println(value);
    }
}
Q14. Can a Java method have multiple parameters?
Q15. What is the scope of a method parameter?
Q16. Which statement correctly describes a parameter?
Q17. Which syntax is used for varargs?
Q18. How is default parameter behavior commonly achieved in Java?
Q19. Does Java directly support default parameters?
Q20. What is a common mistake with method parameters?
Q21. For object arguments, what does Java pass to a method?
Q22. Which statement correctly describes an argument?
Q23. Which is the correct way to declare multiple parameters?
Q24. What will be the output of this code?
public class Test {
    static int add(int... numbers) {
        int total = 0;
        for (int n : numbers) {
            total += n;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(add(10, 20, 30));
    }
}
Q25. What will be the output of this code?
public class Test {
    static void greet(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {
        greet("Ayan");
    }
}

🎉 Great job! Continue learning Java step by step.