Skip to content

Commit 52fbb53

Browse files
committed
feat: Add SwapAnswer class showing mutable array swap and String reassignment
WHAT the code does: - Defines a `SwapAnswer` class demonstrating how to make swaps and updates persist. - Uses an **array** to swap integers: - `swap(int[] arr)` exchanges `arr[0]` and `arr[1]`. - Works because arrays are mutable objects in Java. - For Strings: - `changeName(String naam)` returns a new string. - Caller explicitly reassigns `name` to the return value, making the change visible. WHY this matters: - Shows how to overcome Java’s **pass-by-value** semantics: - Mutable objects (like arrays) allow persistent modifications. - Immutable objects (like Strings) require reassignment. - Reinforces concepts of **mutability vs immutability** in Java. HOW it works: 1. Array `{10, 20}` passed to `swap()`. - After swap → `{20, 10}`. - Printing shows `"20 10"`. 2. String `"Swapping numbers"` passed to `changeName()`. - Returns new string. - Caller assigns it back to `name`. - Printing shows updated string message. Tips & gotchas: - Arrays provide mutability, but introducing a custom wrapper class could make swaps clearer. - For Strings, since they are immutable, always return a new value if modification is required. - Be mindful of side effects: changing arrays inside methods alters caller’s copy too. - Demonstrates a common interview pitfall: difference between reassigning a reference and mutating an object. Use-cases: - Educational example on **mutability and method behavior**. - Foundation for explaining why wrapper objects (like `AtomicInteger`) are used for pass-by-reference-like behavior. - Practical in cases where modifications must reflect outside method scope. - Useful exercise for distinguishing **Java immutability (String)** from **mutability (array)**. Short key: class-swap answer-array-mutable-string-reassign. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 96e1607 commit 52fbb53

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class SwapAnswer {
2+
public static void main(String[] args) {
3+
int[] nums = {10, 20};
4+
// use array so changes reflect
5+
swap(nums);
6+
System.out.println(nums[0] + " " + nums[1]);
7+
8+
String name = "Swapping numbers";
9+
name = changeName(name);
10+
System.out.println(name);
11+
}
12+
13+
// swap numbers method
14+
static void swap(int[] arr) {
15+
int temp = arr[0];
16+
arr[0] = arr[1];
17+
arr[1] = temp;
18+
// this change will be valid since array is mutable
19+
}
20+
21+
// change name method
22+
static String changeName(String naam) {
23+
return "Declaring a method(outside the main method) and called this method inside the main method.";
24+
}
25+
}
26+
27+
/*
28+
🔹 Why numbers don’t swap:
29+
In Java, method parameters are always passed by value.
30+
When you call swap(a, b), the values 10 and 20 are copied into num1 and num2.
31+
Inside the method, num1 and num2 do swap, but those are just local copies.
32+
The original a and b in main remain unchanged.
33+
34+
👉 That’s why after the swap call, printing a and b still shows 10 20.
35+
36+
🔹 Why name doesn’t change
37+
A String in Java is an immutable object (it can’t be modified once created).
38+
When you pass name to changeName, a copy of the reference is passed.
39+
Inside the method, you reassign naam to point to a new string, but this doesn’t affect the original name variable in main.
40+
41+
So after the method call, name still points to "Swapping numbers".
42+
43+
👉 That’s why printing name still shows "Swapping numbers".
44+
45+
📌 Summary in one line:
46+
Primitives like int only swap local copies, and String can’t be changed because it’s immutable and reassigning
47+
the reference inside a method doesn’t affect the original.
48+
*/

0 commit comments

Comments
 (0)