Difficulty: Medium
Correct Answer: By value, passing copies of primitive values and copies of object references
Explanation:
Introduction / Context:
Many developers are confused about how Java passes arguments to methods, especially when dealing with objects. Some languages support pass by reference, where a method can change which object a caller variable refers to. Java follows a simpler rule, but the behaviour of object references can make it seem more complex. This question asks you to identify the correct description of argument passing in Java within a single Java Virtual Machine.
Given Data / Assumptions:
Concept / Approach:
Java uses pass by value for all argument passing. For primitive types, the value itself is copied into the method parameter. For reference types, the value that is copied is the reference, which is like a pointer or handle to the object. Because the reference value is copied, methods can use that reference to modify the object state, but they cannot change the caller variable to refer to a different object. This is why some people mistakenly call Java pass by reference for objects, but technically it is pass by value of the reference.
Step-by-Step Solution:
Step 1: Consider a method that takes an int parameter; Java copies the numeric value into the method stack frame.Step 2: Any changes to that parameter inside the method do not affect the original variable in the caller.Step 3: Now consider a method that takes an object parameter such as a List; Java copies the reference value that points to the List object.Step 4: The method can use that reference to add or remove elements from the List, which affects the shared object seen by the caller.Step 5: However, if the method assigns a new object to that parameter, the caller variable remains unchanged, proving that only the reference value was passed by value.
Verification / Alternative check:
You can verify this behaviour with simple code experiments. Pass a primitive to a method that increments it and observe that the original value does not change. Pass an object and modify its fields; the caller sees the updated state. But if the method reassigns its parameter to a new object, the caller still refers to the original object. These tests are consistent only with pass by value semantics, where object references are copied, not with true pass by reference.
Why Other Options Are Wrong:
Option A claims Java passes by reference, which would allow a method to rebind the caller variable to a different object. This does not happen in Java. Option C suggests that the behaviour may vary by JVM vendor, but the language specification defines argument passing consistently as pass by value. Option D introduces network messages, which are relevant for remote method calls but not for ordinary in process calls. None of these alternatives match the official semantics.
Common Pitfalls:
A common pitfall is misunderstanding the interaction between references and object state, leading to bugs where developers expect reassignment to change the caller variable. Another mistake is teaching that Java is pass by reference for objects, which causes conceptual confusion later when refactoring code. The safest mental model is that Java always passes by value, with object references treated as values that point to shared objects whose internal state may be modified.
Final Answer:
Correct answer: By value, passing copies of primitive values and copies of object references
Discussion & Comments