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.
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:
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:
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.
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.
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.
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
- '
Stringis 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
Stringby reference. - Java always passes arguments by value, including object references.
- '
Stringis immutable, so methods cannot change its contents in place.' - Use
StringBuilderfor mutable text or return a newStringwhen replacement is needed. - Distinguish between mutating an object and rebinding a caller’s variable.
Related reading
- Passing JVM args to Docker image of Spring boot app on Kubernetes
- Paste a multi-line Java String in Eclipse
- PathVariable in SpringBoot with slashes in URL
- Patterns/Principles for thread-safe queues and master/worker program in Java
- peer to peer System with remote method invocation(rmi)
- Performance - Spring Boot - Server Response Time
- Performance Apache HttpAsyncClient vs multi-threaded URLConnection
- Performance issue Java vs C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.