JavaIntermediate#fundamentals

What is the difference between pass-by-value and pass-by-reference in Java?

Java is always pass-by-value. For objects, the value passed is a copy of the reference, so mutating the object's state through it affects the original, but reassigning the parameter does not affect the caller's reference.

Example
void modify(StringBuilder sb) { sb.append("x"); sb = new StringBuilder("new"); }
StringBuilder s = new StringBuilder("a");
modify(s);
System.out.println(s); // "ax" — append worked, reassignment didn't

Related Questions

1
JavaBeginner#fundamentals

What is the difference between static and instance methods?

Open
2
JavaBeginner#oop

What is a constructor and how does constructor overloading work?

Open
3
JavaIntermediate#oop

What is the difference between instance initializer blocks and constructors?

Open