In Java, are parameters passed by reference or by value?
Hi everyone! I just started learning Java and encountered some confusion. Books say that in Java everything is passed by value, but in practice I see something strange.
Here's an example. When I pass an object (e.g., a list) to a method and modify it inside the method, the changes persist after exiting the method:
```java
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
changeString(sb);
System.out.println(sb); // Output: Hello World
}
public static void changeString(StringBuilder s) {
s.append(" World");
}
}
```
Here sb was changed. I thought that if a copy is passed, the changes should be local. So it turns out that objects are passed by reference?
But if I try to assign
No answers yet.
Log in to leave an answer