Java Questions 41 - 50  «Prev  Next»

Java SE 25 Interview Questions: Pass-by-Value and String References

Understand what Java copies into parameters and why object mutation differs from reassigning a caller's variable.

  1. What happens when a String operation appears to modify a string?

    Answer: String content is immutable. Methods return a result rather than changing the original content. Some operations may return the same instance when no change is needed; they do not invariably allocate or consult the intern pool.

    text = text.toUpperCase(java.util.Locale.ROOT); explicitly assigns the result to text. Calling toUpperCase without assigning or using its result does not update the variable.

  2. What does a reference variable hold?

    Answer: It holds a reference value identifying an object, or null. It does not contain the whole object or another variable's storage location.

    The exact runtime encoding is not specified as a portable address format. Two reference values can identify the same object even though they are stored in different variables.

  3. What is passed when a reference-valued argument is supplied to a method?

    Answer: The argument expression is evaluated and its reference value is copied into the parameter variable. Java passes arguments by value, including reference values.

    The object itself is not copied by parameter passing. This allows the caller and callee to access the same object while retaining separate reference variables.

  4. What do a caller's reference and the receiving parameter initially share?

    Answer: They initially have equal reference values when the argument came from that caller variable. If the value is non-null, both identify the same object.

    They are separate variables. Reassigning the parameter to another object or null changes the parameter, not the variable from which the argument value was obtained.

  5. Do caller and callee access the same object?

    Answer: Yes, when the copied reference is non-null and no relevant reassignment has changed which object is used. A mutation through that shared reference affects the one underlying mutable object.

    This does not mean the method can replace the caller's local variable. Sharing an object and sharing a variable's storage are different mechanisms.

  6. Is Java pass-by-value for both primitives and references?

    Answer: Yes. A primitive argument supplies a primitive value; a reference argument supplies a reference value. Both are copied into parameter variables.

    Changing a primitive parameter does not change the caller's variable. A reference parameter can provide access to shared mutable state, but reassigning the parameter still does not replace the caller's variable.

  7. Does passing a reference make a copy of the object?

    Answer: No. Only the reference value is copied. If an independent object is required, an explicit copying operation must be designed, including whether nested mutable objects should also be copied.

    For arrays, clone creates a new array, but a reference array's elements initially point to the same element objects. Parameter passing itself does not even perform that shallow array copy.

  8. What can a method change under pass-by-value?

    Answer: It can change its own parameter variables and any accessible mutable state reached through references. It cannot replace a caller's local variable merely by assigning to its corresponding parameter.

    public class ReferencePassing {
        static void change(StringBuilder value) {
            value.append("B");
            value = new StringBuilder("Other");
            value.append("C");
        }
        static void replace(String value) {
            value = "Changed";
        }
        public static void main(String[] args) {
            StringBuilder builder = new StringBuilder("A");
            change(builder);
            System.out.println(builder);
            String text = "Original";
            replace(text);
            System.out.println(text);
        }
    }

    The output is AB and Original. The append affects the shared builder; the parameter reassignments do not affect the caller's variables.

  9. Is mutating an object through a parameter evidence of pass-by-reference?

    Answer: No. Java's copied reference value allows access to the same object, so mutation can be visible to the caller. Java still uses pass-by-value.

    A true pass-by-reference parameter would alias the caller's variable itself, allowing an assignment to replace that variable's value. Java does not provide that parameter-passing mode. Returning a result or deliberately mutating a holder are different designs.

  10. Can a callee reassign the caller's original reference variable?

    Answer: Not by reassigning the parameter. To use a replacement result, the caller can assign a returned reference, for example value = transform(value);.

    A method may independently have access to a shared field or array element and assign it, but that is ordinary mutation of shared state. It does not turn parameter passing into pass-by-reference.

References: JLS: argument evaluation and method invocation, JLS: values, and String API.