0

Is it possible to pass Array by Value in a function in Java. When we pass array in a function in default it is pass by reference and original version of data in array gets modified.

Hence would like to know if there is pass by value option in Java?

user1925406
  • 683
  • 3
  • 15
  • 32

1 Answers1

5

No, Java doesn't have that, but you can make a defensive copy...

public void doSomethingWithArray(int[] arr) {
    int[] myArr = Arrays.copyOf(arr, arr.length);
     ...
}
Todd
  • 28,544
  • 10
  • 77
  • 83