0

parameter

Hey, For example I have the following code. public class SpecialArray { // fibonacci public static boolean isFibonacciArray(int arr[]) { for (int i = 2; i < arr.length; i++) if (arr[i] != arr[i - 1] + arr[i - 2]) return false; return true; } public static void main(String[] args) { int array[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; boolean isFibonacci = isFibonacciArray(array); if (isFibonacci == true) System.out.println("Fibonnaci"); else System.out.println("NOT fibonnaci"); } } I want to know if inside the Main I have, for example, the variable int[] array, if I call the function then the array goes into the parameter which is int[] arr in the boolean function and then everything that is actually related to the int[] arr becomes the int[] array I have in the main? Which means that in fact the int[] arr found in the for loop and also inside the if becomes an int[] array of the main function? Thanks for help!

7th Aug 2024, 8:54 AM
RaymanCoder
1 Réponse
+ 1
RaymanCoder I think you are asking whether the array that gets passed from main into the function is a separate copy of the array, or is a reference to the original array. In Java, arrays get passed as a reference to the original array. So if the function code were to modify the array, then it would change the same array that main uses. For primitive data types, like int or float, a copy of the value gets passed. Changing those parameters inside the function will not affect the original caller's value.
7th Aug 2024, 12:10 PM
Brian
Brian - avatar