0

What is "new" in array?? Why is it used??

int[ ] intArr = new int[5]; System.out.println(intArr.length);

12th Sep 2017, 2:11 AM
Saurabh Thakur
Saurabh Thakur - avatar
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};
12th Sep 2017, 3:15 AM
Rrestoring faith
Rrestoring faith - avatar
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
12th Sep 2017, 2:13 AM
Morpheus
Morpheus - avatar
0
yes
12th Sep 2017, 2:15 AM
Saurabh Thakur
Saurabh Thakur - avatar
0
what if I don't use it??
12th Sep 2017, 2:16 AM
Saurabh Thakur
Saurabh Thakur - avatar
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
12th Sep 2017, 2:17 AM
Morpheus
Morpheus - avatar
0
thanks!
12th Sep 2017, 2:18 AM
Saurabh Thakur
Saurabh Thakur - avatar
- 1
reply for more
12th Sep 2017, 2:15 AM
Morpheus
Morpheus - avatar