My code works fine here, but to my mind, the exception should be thrown. My function is continuously invoking itself. Eventually, it is being called on the last element in the array and adds that number. Then, I make a copy of the array from the next position to the end. But since I do not have that next element, so I cannot make a copy of the array starting from the subsequent one (I am already at the very last position), why is it working?
System.out.println(sumOfArrayRecursively(new int[]{5, 9, 45, 6, 2, 7, 34, 8, 6, 90, 5, 243}));
public static int sumOfArrayRecursively(int [] arr){
if(arr.length == 0) return 0;
else{
return arr[0] + sumOfArrayRecursively(Arrays.copyOfRange(arr, 1, arr.length));
}
}