0
What is "new" in array?? Why is it used??
int[ ] intArr = new int[5]; System.out.println(intArr.length);
7 Answers
+ 6
The new keyword is used to create an instance of the class.
Or in other words:
'new' creates an object.
In this case, we are creating the array.
There are some scenerios where this keyword can be ommited:
int[] b = {5,4,2};
This is purely 'syntax sugar'.
Because it is the same as doing:
int[] b = new int[]{5,4,2};
0
it's use to allocate memory dynamically at run time,
means that your array won't take space during compilation but only when u run it , that's used for memory efficiency
0
yes
0
what if I don't use it??
0
then no problem but your array will take memory of 5 ints before even running of program, nothing bad if u got lot of memory, but is a problem in large arrays
0
thanks!
- 1
reply for more