0

I am new to java programming. I was running the following code:

public class Main {

  public static void main(String[] args) {
    int arr[] = { 1, 2, 3 };
    int a = 10;
    change(arr);
    for (int i = 0; i < arr.length; i++) {
      System.out.println("arr[] = " + arr[i]);
    }
    change2(a);
    System.out.println("a = " + a);
  }

  static void change(int[] nums) {
    nums[0] = 99;
  }

  static void change2(int nums) {
    nums = 99;
  }
}

Output:

arr[] = 99
arr[] = 2
arr[] = 3
a = 10

When I run this I noticed that arr[0] which was initialized as 1 now has the value 99. However when the exact same logic is applied to a which was initialized as 10, it still has the value 10. Can someone explain this to me?

  • It might help you to see see how it works if you try to completely assign a new array and then see what happens – g00se Sep 26 '22 at 14:07

0 Answers0