In: Computer Science
Java uses pass by value to pass parameters to a method. What are the important differences between passing a value of variables of primitive data types and passing arrays? Use a specific example to explain
When we do pass by value with primitive than changes made at that method will not have impact at caller side
where if we pass array as call by value than changes made at that method will have impact it caller side
Example:
public class CallByValue {
public static void main(String[] args) {
int arr[]= {1,2};
int x=10;
System.out.println("Before Call: ");
System.out.println(x);
System.out.println(arr[0]+" "+arr[1]);
change(x);
change(arr);
System.out.println("After Call: ");
System.out.println(x);
System.out.println(arr[0]+" "+arr[1]);
}
private static void change(int[] arr) {
arr[0]=100;
arr[1]=200;
}
private static void change(int x) {
x=100;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME