So I'm new to Java.
I encountered this question while doing exercises.
My Solution
public static void fillWithRandomNumbers (double[] values) {
double[] numbers = new double[values.length];
for(int i = 0 ; i < numbers.length ; i++){
numbers[i] = Math.random();
values[i] = numbers[i]; // <---- That's the only thing I changed from the above code.
}
}
Since from my lecture notes it states that.
In Java, the assignment operator may be used to copy primitive data type variables, but not arrays.
But when I tried to run the code written in the question above and print the results. It gives me the full random number output.
Output
--------------------Configuration: Test - JDK version 1.8.0_171 <Default> - <Default>--------------------
0.5879563734452546
0.18409227033985642
0.7866217402158342
0.8023851737037745
8.892207358659476E-4
0.8614310541417136
0.6907363995600082
0.8974247558057734
0.4294464665985942
0.19879131710939557
Process completed.
The Full Code From The Above Output.
public class Test {
public static void main(String[] args) {
double[] test = new double[10];
fillWithRandomNumbers(test);
}
public static void fillWithRandomNumbers(double[] values){
double[] numbers = new double[values.length];
for(int i = 0 ; i < numbers.length ; i++){
numbers[i] = Math.random();
values = numbers;
System.out.println(values[i]);
}
}
}
My Questions is how is that possible? Since in the notes it stated that array contents can't be copied by direct assigning between arrays. This also applies to C if I'm not mistaken.
And also is my solution correct? I just want to understand how and why is this possible?

