Code a calling and called a method that demonstrates that actual parameters, that are variables of value types, do not have side effects but those of reference types can have such effects. Include appropriate display instructions to prove the point. Use a parameter that is an array of int as your reference type and an element of this array as your value type.
Use java and explain the code, thanks
CODE
public class Main {
public static void something(int arr[], int x) {
System.out.println("Changing the element " + x + " present in the array to 10..");
x = 10;
System.out.println("Doubling every element of the array...");
for (int i=0; i<arr.length; i++) {
arr[i] *= 2;
}
}
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
System.out.println("Array before calling the method....");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
int x = arr[1];
System.out.println("x before calling the method: " + x);
System.out.println("\n");
something(arr, x);
System.out.println("Array after calling the method....");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("x after calling the method: " + x);
}
}
Get Answers For Free
Most questions answered within 1 hours.