Introduction
Parameters are variables that receive values when a method is called. They allow methods to accept input.
Syntax
returnType methodName(type param1, type param2, ...) {
// Use parameters
}
Basic Example
class Calculator {
// Parameters: a and b
int add(int a, int b) {
return a + b;
}
void display(String message) {
System.out.println(message);
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.add(10, 20); // Arguments: 10, 20
calc.display("Hello"); // Argument: "Hello"
}
}
Parameters vs Arguments
- Parameters: Variables in method definition
- Arguments: Actual values passed when calling
class Example {
// x and y are parameters
void method(int x, int y) {
System.out.println(x + y);
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example();
obj.method(5, 10); // 5 and 10 are arguments
}
}
Types of Parameter Passing
1. Pass by Value (Primitives):
Copy of value is passed. Original value not affected.
class Test {
void modify(int x) {
x = x * 2;
System.out.println("Inside method: " + x);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
int num = 10;
t.modify(num);
System.out.println("Outside method: " + num);
}
}
Output:
Inside method: 20
Outside method: 10 // Original unchanged
2. Pass by Value (Objects):
Reference copy is passed. Object can be modified.
class Student {
String name;
int marks;
}
class Test {
void modify(Student s) {
s.marks = 100; // Modifies original object
System.out.println("Inside: " + s.marks);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.marks = 50;
Test t = new Test();
t.modify(student);
System.out.println("Outside: " + student.marks);
}
}
Output:
Inside: 100
Outside: 100 // Original object modified
Multiple Parameters
class Student {
void setDetails(String name, int rollNo, double marks) {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setDetails("John", 101, 85.5);
}
}
Variable Number of Parameters (Varargs)
Allows passing variable number of arguments.
Syntax:
returnType methodName(type... paramName) {
// paramName is treated as array
}
Example:
class Calculator {
int sum(int... numbers) { // Varargs
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.sum(10)); // 1 argument
System.out.println(calc.sum(10, 20)); // 2 arguments
System.out.println(calc.sum(10, 20, 30)); // 3 arguments
System.out.println(calc.sum(5, 10, 15, 20)); // 4 arguments
}
}
Varargs Rules
1. Must be Last Parameter:
// Correct
void method(int x, String... names) { }
// Wrong
// void method(String... names, int x) { } // ❌ Error
2. Only One Varargs:
// Wrong
// void method(int... a, String... b) { } // ❌ Error
// Correct
void method(int... numbers) { }
Default Parameter Values
Java doesn’t support default parameter values directly.
Workaround - Method Overloading:
class Example {
// Method with all parameters
void display(String name, int age) {
System.out.println(name + ", " + age);
}
// Method with default age
void display(String name) {
display(name, 18); // Default age = 18
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example();
obj.display("John", 25);
obj.display("Alice"); // Uses default age
}
}
Primitive vs Reference Parameters
Primitive Types:
void modify(int x) {
x = 100; // Does NOT change original
}
int num = 50;
modify(num);
System.out.println(num); // Still 50
Reference Types:
void modify(int[] arr) {
arr[0] = 100; // DOES change original
}
int[] array = {1, 2, 3};
modify(array);
System.out.println(array[0]); // Now 100
Complete Example
class Calculator {
// No parameters
void sayHello() {
System.out.println("Hello!");
}
// Single parameter
int square(int num) {
return num * num;
}
// Multiple parameters
int add(int a, int b) {
return a + b;
}
// Reference parameter
void modifyArray(int[] arr) {
arr[0] = 999;
}
// Varargs
int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.sayHello();
System.out.println("Square: " + calc.square(5));
System.out.println("Sum: " + calc.add(10, 20));
int[] arr = {1, 2, 3};
calc.modifyArray(arr);
System.out.println("Modified: " + arr[0]);
System.out.println("Total: " + calc.sum(1, 2, 3, 4, 5));
}
}
Parameter Types Summary
| Type | Example | Notes |
|---|---|---|
| Primitive | void method(int x) | Pass by value |
| Object | void method(Student s) | Reference passed |
| Array | void method(int[] arr) | Reference passed |
| String | void method(String s) | Immutable |
| Varargs | void method(int... x) | Variable arguments |
Quick Reference
class Example {
// Single parameter
void method1(int x) { }
// Multiple parameters
void method2(int x, String y) { }
// Varargs
void method3(int... numbers) { }
// Mixed
void method4(String name, int... scores) { }
}
// Calling
Example obj = new Example();
obj.method1(10);
obj.method2(5, "Hello");
obj.method3(1, 2, 3, 4);
obj.method4("John", 85, 90, 95);
Exam Tips
Remember:
- Parameters: Variables in method definition
- Arguments: Values passed when calling
- Primitives: Pass by value (copy)
- Objects: Reference passed (can modify)
- Varargs: Variable number of arguments
- Varargs must be last parameter
- Only one varargs per method
- Multiple parameters separated by comma
- Java has no default parameters
- Use overloading for defaults
Common Questions:
- What are parameters?
- Difference between parameter and argument?
- What is pass by value?
- Can objects be modified via parameters?
- What are varargs?
- Rules for varargs?
- Does Java support default parameters?