...
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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));
} |
...