Understanding Java: Pass‑by‑Value vs Pass‑by‑Reference Explained
This article demystifies Java’s parameter passing mechanism by clarifying the distinction between primitive and reference types, illustrating how assignment and method calls affect values and objects, and explaining the underlying JVM memory model, stack, heap, and array handling with clear code examples.
Do not get hung up on the literal meanings of "Pass By Value" and "Pass By Reference"; instead focus on the concrete behavior of Java’s parameter passing.
1: Distinguish Primitive and Reference Types
int num = 10;
String str = "hello";As shown, num is a primitive type whose value is stored directly in the variable, while str is a reference type that stores only the address of the actual String object.
2: Understand the Assignment Operator (=)
num = 20;
str = "java";For the primitive num, the assignment operator directly changes the stored value, overwriting the previous one. For the reference str, the operator changes the address stored in the variable, but the original String object remains unchanged.
3: What Happens When a Method Is Called?
Parameter passing is essentially an assignment operation.
Example with a primitive type:
void foo(int value) {
value = 100;
}
foo(num); // num remains unchangedExample with a reference type that does not modify the object:
void foo(String text) {
text = "windows";
}
foo(str); // str remains unchangedExample with a reference type that modifies the object:
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
builder.append("4");
}
foo(sb); // sb becomes "iphone4"Example where the method reassigns the parameter without affecting the original reference:
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
builder = new StringBuilder("ipad");
}
foo(sb); // sb remains "iphone"Local Variables and Method Parameters in the JVM
Both local variables and method parameters are stored on the stack. In a 32‑bit JVM, primitive types and references occupy 4 bytes, while long and double occupy 8 bytes. Each thread has its own stack, while the heap is shared among all threads.
When an object is created with new, memory is allocated on the heap and the reference stored on the stack points to that heap object.
Array Types, References, and Objects
Arrays are objects; the variable holding an array is a reference. For example, int[] arr = new int[10] allocates an array object on the heap and stores its address in arr on the stack.
Multidimensional arrays are arrays of arrays. int[][] arr2 = new int[2][4] creates a top‑level array of length 2, each element referencing a length‑4 int array.
When an array reference is passed to a method, the method can modify the array’s elements but cannot reassign the reference to a new array.
About String
A String object internally holds a char[], a start index, and a length. Although the underlying character array can be shared, String is immutable, so its logical state never changes after creation.
Literal strings may be stored in the string constant pool, which can reside in the heap or the method area depending on the JVM implementation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
