Consider following array declarations,
int[] num2 = {1, 2, 3, 4, 5};
int[] num3 = new int[]{1, 2, 3, 4, 5};
System.out.println(Arrays.toString(num2));
System.out.println(Arrays.toString(num3));
So for the
num3if I passed directly the right hand side declaration to a functions its valid.i.e.
Arrays.toString(new int[]{1,2,3})But Why can't thenum2approach
if I call someFunction({1,2,3,4,4}) it throws an Error as
illegal start of an expression.
If both declarations are valid then why can't I use it in arguments of method ?
I was trying to simply the approach , when passing some temporary array to a function such as
somefunction(new int[]{1,2,3,4})
Is it possible simplify this in java ? any java 8 tricks ?