Passing Objects to Methods in Java - Quiz
Total: 5 questions
1. Is Java pass by value or pass by reference when objects are passed to methods?
Is Java pass by value or pass by reference when objects are passed to methods?
Java is strictly pass by value. For a primitive parameter the value itself is copied; for an object parameter a copy of the reference is copied, that is the address of the object on the heap. The copied reference still points at the very same object, so the method can change that object's state and every holder of the reference sees the change. What the method cannot do is make the caller's variable point at a different object. Java has no pass-by-reference like C++ or C#; the common phrase "objects are passed by reference" is a convenient shortcut worth spelling out in an interview.
2. Why are changes made to an object's fields inside a method visible to the caller, while changes to a primitive parameter are not?
Why are changes made to an object's fields inside a method visible to the caller, while changes to a primitive parameter are not?
Every method call allocates a new stack frame holding the parameters as local variables. For a primitive the value itself is copied into that frame: the parameter and the caller's variable are different memory cells, so a *= 2; inside the method has no effect on x outside. For an object only a copy of the reference goes on the stack, while the object exists in a single instance on the heap. The statement box.width *= 2; modifies that heap object, and the caller — which looks at the very same object — sees the new value.
static void changeObject(Box box) {
box.width *= 2; // visible in main()
}
static void changePrimitives(int a) {
a *= 2; // NOT visible in main()
}
3. Why is assigning a new object to a parameter inside a method invisible to the calling code?
Why is assigning a new object to a parameter inside a method invisible to the calling code?
Because the parameter is a local copy of the reference. The line box = new Box(1, 1, 1); makes only that copy point at a new object, while the caller's variable keeps pointing at the old one. If objects really were passed by reference, the outer variable would move to the new object too — but in Java it does not:
static void reassign(Box box) {
box = new Box(1, 1, 1); // only the local copy of the reference changes
box.width = 100;
}
static void mutate(Box box) {
box.width = 100; // modifies the object created in main()
}
after reassign: 5.0
after mutate: 100.0
The same rule explains why a swap() method cannot exchange two of the caller's variables: it only swaps its own local parameters.
4. How do an array, a String and a StringBuilder behave when they are passed to a method?
How do an array, a String and a StringBuilder behave when they are passed to a method?
An array in Java is an ordinary object, so numbers[0] = 42; inside a method modifies the same array and the caller sees the new value. The same is true for StringBuilder and collections: builder.append(" Java") changes the object itself. String, however, is immutable: text += " Java" creates a new string and assigns it to the local parameter, leaving the original untouched. If a string has to grow inside a method, use StringBuilder or return the result.
numbers[0]: 42
text: Hello
builder: Hello Java
5. What does a method with a class as its return type actually return, and what happens to an object created inside that method?
What does a method with a class as its return type actually return, and what happens to an object created inside that method?
It returns a reference to the object on the heap — the object itself is never copied on return. An object created inside a method does not disappear when the method ends: it stays on the heap as long as at least one reference to it exists, and only then becomes eligible for garbage collection.
ReturnObjectExample incrementByTen() {
return new ReturnObjectExample(a + 10); // a reference to the new object is returned
}
ReturnObjectExample ob1 = new ReturnObjectExample(2);
ReturnObjectExample ob2 = ob1.incrementByTen();
System.out.println(ob1.a + " " + ob2.a); // 2 12
This is also how you "return several values" from one method: wrap them into a container — your own class, a record since Java 16, an array or a collection.