Today what i learned in class
- define parameter- A parameter in Java is a variable that is declared in a method or constructor definition and is used to accept input values when the method is called
Simple Meaning
- It is like a placeholder for data.
- It lets you pass information into methods.7
class Example {
// 'a' and 'b' are parameters (formal parameters)
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
// 5 and 10 are arguments (actual parameters)
int result = add(5, 10);
System.out.println("Sum: " + result);
}
}
- Parameters →
int a, int b
(declared in method definition) - Arguments →
5, 10
(values passed while calling the method)
Why Are Parameters Used?
- To pass data into a method.
- To make methods reusable with different values.
- To avoid repeating the same code with different hardcoded values.
Top comments (0)