I am pretty new to Java and one topic just blew my mind. Passing array to a java function. Code is as follows:
private void PassArray(){
String[] arrayw = new String[4]; //populate array
PrintA(arrayw);
}
private void PrintA(String[] a){
//do whatever with array here
}
They say in java arrays are passed by reference. But it looks so much similar to "Call by Value" in C:
#include <stdio.h>
/* function declaration */
double getAverage(int arr[], int size);
int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;
/* output the returned value */
printf( "Average value is: %f ", avg );
return 0;
}
double getAverage(int arr[], int size)
{
int i;
double avg;
double sum;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = sum / size;
return avg;
}
Please explain the difference between them in terms of value,reference and memory allocations. Any further links will be appreciated.