Tsallaka zuwa abun ciki
Justice Tech School

Pass by Value

Java passes copies. For objects the copy is a reference, so changes to the object are seen but reassigning is not.

Example
public class Main {
  static void change(int n, int[] arr) { n = 99; arr[0] = 99; }
  public static void main(String[] args) {
    int x = 1;
    int[] a = {1};
    change(x, a);
    System.out.println(x + " " + a[0]);
  }
}

Result

1 99

Exercise

Java always passes by ___.

Java always passes by 

Quiz Test

Is the int x changed by the method?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.