0

arrays

why sometimes we put ...=new int[5] for exapmle and sometimes no!

5th Aug 2017, 3:07 PM
khouloud jdidi
khouloud  jdidi - avatar
1 ответ
+ 4
int[] someArray = new int[5] - allocates an array in memory with 5 zero values and assign it to someArray pointer. After this allocation you can work with this array: write and read values. If you write just int[] someArray; than you have only a pointer that is not initialized, and you can't use it in till you assign to it some array or null value. So you will need to initialize it later in the code. Also if it is a field it will be initialized automatically with null value.
5th Aug 2017, 3:21 PM
Roman
Roman - avatar