๐ Quick Study Guide
๐ Actual Parameters (Arguments): These are the values you pass to a method when you call it. Think of them as the real data that the method will use.
๐ก Formal Parameters: These are the variables declared in the method's definition that receive the values of the actual parameters.
๐ Parameter Matching: The number, type, and order of actual parameters must match the formal parameters defined in the method signature.
๐งฎ Pass by Value: Java uses pass by value. This means that when you pass a variable as an argument, a copy of the variable's value is passed to the method. Changes to the formal parameter inside the method do not affect the original variable.
๐ง Primitive Types: For primitive types (int, double, char, boolean), the actual value is copied.
๐ฆ Reference Types: For objects, the reference (memory address) is copied, not the object itself. Therefore, the method can modify the object's state.
๐ Example:
java
public class Example {
public static void modifyValue(int x) {
x = x + 10;
System.out.println("Inside method: x = " + x); // Prints 20
}
public static void main(String[] args) {
int y = 10;
modifyValue(y); // y is the actual parameter
System.out.println("Outside method: y = " + y); // Prints 10
}
}
Practice Quiz
1. Which of the following best describes an actual parameter in Java?
- A) A variable declared inside a method.
- B) The value passed to a method when it is called.
- C) The data type of a method's return value.
- D) The name of the method itself.
2. What happens when you pass a primitive type as an actual parameter in Java?
- A) The original variable is directly modified.
- B) A copy of the variable's value is passed.
- C) The method cannot access the variable.
- D) An error occurs during compilation.
3. If a method signature is `void calculate(int a, double b)`, which of the following method calls is valid?
- A) `calculate(3.14, 5);`
- B) `calculate(10);`
- C) `calculate(5, 3.14);`
- D) `calculate(5, 3.14, 2);`
4. What is the term for the variables listed in a method's definition that receive the values of the actual parameters?
- A) Global variables
- B) Instance variables
- C) Formal parameters
- D) Local variables
5. In Java, when passing an object as an actual parameter, what is actually passed to the method?
- A) A copy of the entire object.
- B) The object's memory address (reference).
- C) Only the primitive data fields of the object.
- D) Nothing, objects cannot be passed as parameters.
6. Consider the following code:
java
public class Example {
public static void changeValue(int[] arr) {
arr[0] = 100;
}
public static void main(String[] args) {
int[] myArray = {1, 2, 3};
changeValue(myArray);
System.out.println(myArray[0]);
}
}
What will be printed to the console?
- A) 1
- B) 2
- C) 3
- D) 100
7. Which of the following statements about actual and formal parameters is FALSE?
- A) The number of actual parameters must match the number of formal parameters.
- B) The types of actual parameters must be compatible with the types of formal parameters.
- C) The names of actual parameters must match the names of formal parameters.
- D) The order of actual parameters must match the order of formal parameters.
Click to see Answers
- B
- B
- C
- C
- B
- D
- C