Java
String
Passing by Reference
Programming Concepts
Java Tips

Passing a String by Reference in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

You cannot pass a Java String by reference in the C++ sense. Java always passes arguments by value, and for objects that value is a copy of the reference, which means a method can use the same object the caller points to but cannot reassign the caller’s variable itself.

What Java Actually Passes

Java has only pass-by-value semantics.

For primitives, the copied value is the primitive itself. For objects, the copied value is the reference.

That means this method cannot replace the caller’s String variable:

java
1public class Demo {
2    static void change(String text) {
3        text = "new value";
4    }
5
6    public static void main(String[] args) {
7        String value = "old value";
8        change(value);
9        System.out.println(value);
10    }
11}

The output is still old value because text is only a local copy of the reference.

Why Strings Make This More Obvious

Strings in Java are immutable. Even if the method receives a reference to the same String object, it cannot mutate the object’s characters in place. Any apparent modification creates a new String object instead.

So this line:

java
text = text + "!";

creates a different String and reassigns only the local parameter variable.

If You Need Mutable Text

If the goal is to let a method modify text-like content visible to the caller, use a mutable type such as StringBuilder.

java
1public class Demo {
2    static void change(StringBuilder text) {
3        text.append(" world");
4    }
5
6    public static void main(String[] args) {
7        StringBuilder value = new StringBuilder("hello");
8        change(value);
9        System.out.println(value);
10    }
11}

This prints hello world because both caller and callee refer to the same mutable object.

If You Need To Replace The Value

If the goal is to compute a new string, the normal Java pattern is to return it.

java
1public class Demo {
2    static String change(String text) {
3        return text + " world";
4    }
5
6    public static void main(String[] args) {
7        String value = "hello";
8        value = change(value);
9        System.out.println(value);
10    }
11}

This is clearer and more idiomatic than trying to simulate pass-by-reference.

Wrapper Objects Are Possible But Rarely Ideal

You can simulate reference-style reassignment with a mutable wrapper.

java
1class StringBox {
2    String value;
3
4    StringBox(String value) {
5        this.value = value;
6    }
7}
8
9public class Demo {
10    static void change(StringBox box) {
11        box.value = "new value";
12    }
13
14    public static void main(String[] args) {
15        StringBox box = new StringBox("old value");
16        change(box);
17        System.out.println(box.value);
18    }
19}

This works, but it is usually less clean than returning a new String or using StringBuilder when mutation is really required.

The Mental Model To Keep

A good rule is:

  • Java never gives a method the power to rebind the caller’s variable directly
  • methods can mutate the object behind a copied reference if that object is mutable
  • 'String is immutable, so only reassignment or replacement is possible'

That is why people often say "Java passes object references by value." The phrase is clumsy, but it points at the right behavior.

Common Pitfalls

The most common mistake is saying Java is pass-by-reference because object changes can be observed by the caller. The caller sees mutations only because both references point to the same mutable object.

Another mistake is using String where mutable text behavior is required. StringBuilder is the better fit for in-place text changes.

A third issue is building wrapper classes only to mimic reference semantics when a simple return value would make the code easier to read.

Summary

  • Java does not pass String by reference.
  • Java always passes arguments by value, including object references.
  • 'String is immutable, so methods cannot change its contents in place.'
  • Use StringBuilder for mutable text or return a new String when replacement is needed.
  • Distinguish between mutating an object and rebinding a caller’s variable.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.