I'm learning java and was told arrays are implemented as objects. But they show two different codes without diving into details.
First they ask us to use arrays like this, but the downside is to manually add the values:
int nums[] = new int[10];
nums[0] = 99;
nums[1] = -622;
.
.
.
Then they use this in some programs saying new is not needed because Java automatically does stuff:
int nums[] = {99, - 10, 100123, 18, - 972 ......}
If the second code is shorter and allows me to use arrays straightaway whats the point of the first code if they do the same thing but the first one require more code to input value by hand.
int nums[] = new int[10]will compile into an object similar toint nums[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}. Therefore, 2nd method is a short-hand for instantiating the array with that fixed size, and putting in the elements. Arrays instantiated with 2nd method can still be changed element by element later on.