OOP AND ENCAPSULATION
StringBuilder Reference Behavior
Java is pass-by-value for references. You get a copy of the reference, not the reference itself.
public class StringBuilderExample {
static void modifyContent(StringBuilder sb) {
sb.append(" modified"); // โ
Modifies the object - caller sees this
System.out.println("Inside method after append: " + sb);
}
static void reassignReference(StringBuilder sb) {
sb.append(" first"); // โ
Modifies original object
sb = new StringBuilder("completely new"); // โ Only changes local copy of reference
sb.append(" content"); // โ Modifies the new object, not original
System.out.println("Inside method after reassign: " + sb);
}
public static void main(String[] args) {
StringBuilder original = new StringBuilder("start");
modifyContent(original);
System.out.println("After modifyContent: " + original); // "start modified"
reassignReference(original);
System.out.println("After reassignReference: " + original); // "start modified first"
// Note: "completely new content" is lost!
}
}
๐ก Learning Tip: You can change the objectโs content through the reference, but you canโt change where the original reference points.