Pass-by-Value in Java
Understand Java's pass-by-value: primitives vs references, reassignment vs mutation, arrays and immutability pitfalls.
Pass-by-Value in Java is a free Java Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.
Pass-by-value overview
Goal: Learn how Java passes parameters. Compare primitives vs references, reassignment vs mutation, arrays, and immutability.
Primitives copy
Primitives are passed by value: the callee gets a copy. Reassigning the parameter does not change the caller's variable.
public class Main {
static void inc(int x) {
x = x + 1; // modifies the local copy only
}
public static void main(String[] args) {
int a = 5;
inc(a);
System.out.println("a after inc(a) = " + a); // still 5
}
}
Refs: mutate vs reassign
For objects, the reference is copied. Mutating through it affects the caller's object; reassigning the parameter does not.
public class Main {
static class Box { int n; }
static void setTo999(Box b) { b.n = 999; } // mutate via copied reference
static void reassign(Box b) { b = new Box(); b.n = 123; } // reassign local copy only
public static void main(String[] args) {
Box bx = new Box();
bx.n = 10;
setTo999(bx);
System.out.println("after setTo999: bx.n = " + bx.n); // 999
reassign(bx);
System.out.println("after reassign: bx.n = " + bx.n); // still 999
}
}
Arrays: mutate vs reassign
Arrays are objects. Mutate to affect the caller; reassigning the parameter does not replace the caller's array.
public class Main {
static void fillZero(int[] a) {
for (int i = 0; i < a.length; i = i + 1) a[i] = 0; // mutate caller array
}
static void replace(int[] a) {
a = new int[] {9, 9, 9}; // reassign local copy of the reference
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
fillZero(arr);
System.out.println("after fillZero: " + arr[0] + "," + arr[1] + "," + arr[2]); // 0,0,0
replace(arr);
System.out.println("after replace: " + arr[0] + "," + arr[1] + "," + arr[2]); // still 0,0,0
}
}
Immutable vs mutable
String is immutable (returns new objects); StringBuilder is mutable (changes the same instance).
public class Main {
static void tryChangeString(String s) {
s = s + "!"; // new String; original unchanged
}
static void changeBuilder(StringBuilder sb) {
sb.append("!"); // mutates same object
}
public static void main(String[] args) {
String s = "hi";
tryChangeString(s);
System.out.println("after tryChangeString: " + s); // "hi"
StringBuilder sb = new StringBuilder("hi");
changeBuilder(sb);
System.out.println("after changeBuilder: " + sb.toString()); // "hi!"
}
}
Swap correctly
Because parameters are copies, swapping primitives inside a method does not affect the caller. Return new values (or a holder object).
public class Main {
static void trySwap(int a, int b) {
int t = a; a = b; b = t; // swaps local copies only
}
static int[] swap(int a, int b) {
return new int[] { b, a }; // return results to the caller
}
public static void main(String[] args) {
int x = 1, y = 2;
trySwap(x, y);
System.out.println("after trySwap: x=" + x + ", y=" + y); // 1,2
int[] res = swap(x, y);
x = res[0]; y = res[1];
System.out.println("after swap: x=" + x + ", y=" + y); // 2,1
}
}
Pass-by-value check
Quick check: Which statement about Java's parameter passing is correct?
Recap & next
Recap: You saw primitives vs references, mutation vs reassignment, arrays as objects, immutability, and a safe swap pattern. Next: Static vs Instance Methods.
Learn Java with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 104
- Lessons
- 374
Frequently Asked Questions
Is the “Pass-by-Value in Java” lesson free?
Yes — the full text of “Pass-by-Value in Java” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.
What will I learn in “Pass-by-Value in Java”?
Understand Java's pass-by-value: primitives vs references, reassignment vs mutation, arrays and immutability pitfalls. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Pass-by-Value in Java” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pass-by-Value in Java
- Static vs Instance Methods
- Method Decomposition & Clean Code