In: Computer Science
[6 marks] 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
Differences between the passing value of variables of primitive data types and passing arrays:
-> While passing a primitive value of variables, you pass a copy
of the value, and while passing arrays you pass a copy of the
handle.
-> Passing value of variables of primitive data types always
have value but in passing arrays can be null.
-> a value of variables of primitive data types is original
while passing arrays value can be modified.
I will explain with the program:
Example :
import java.util.*;
class Example
{
public static void demoFunction(int arr[], int size)
{
System.out.println("Size is "+size);
arr=new int[size];
arr[0]=100;
arr[1]=200;
arr[2]=300;
System.out.println("Array elements: "+Arrays.toString(arr));
}
public static void main(String args[])
{
int size;//primitive
int arr[]={0}; //array
size=3;
demoFunction(arr, size);
}
}
Output: