Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In Java, primitive data types like int, float, double, char, etc., are passed by value. This means that a copy of the actual value is passed to the method, and any change made to that value within the method does not affect the original value outside of the method.

Code Block
languagejava
static void modifyValue(int x) {
    x = x * 2;
    System.out.println("Value inside method: " + x);
}

public static void main(String[] args) {
    int a = 5;
    modifyValue(a);
    System.out.println("Value outside method: " + a);
}

...

Here's an example:

Code Block
languagejava
static void modifyArray(int[] arr) {
    arr[0] = 99;
    System.out.println("Array inside method: " + Arrays.toString(arr));
}

public static void main(String[] args) {
    int[] myArray = {1, 2, 3};
    modifyArray(myArray);
    System.out.println("Array outside method: " + Arrays.toString(myArray));
}

...