+ 1

what does new int[5] mean?

int[]arr=new int[5] what does it mean? can someone please explain it to me? it would be better if you explain with an example â˜ș

21st Apr 2018, 9:30 AM
RH Tasin
RH Tasin - avatar
4 Answers
+ 3
class Main { public static void main(String[] args) { //This is how to declare an array in java,this array is of //type integer and has 3 elements int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; System.out.println(arr[0]);//0 System.out.println(arr[1]);//1 System.out.println(arr[2]);//2 System.out.println(arr[3]);//outputs an error } }
21st Apr 2018, 10:29 AM
HBhZ_C
HBhZ_C - avatar
+ 2
so new int[] mean I can add only 3 arr?
23rd Apr 2018, 5:37 AM
RH Tasin
RH Tasin - avatar
+ 2
new int[] means initialize an array object named arr and has a given number of elements,you can choose any number you want,but it will be of the type declared yet.
24th Apr 2018, 5:36 PM
HBhZ_C
HBhZ_C - avatar
0
It basically mean " create an array of integer(number) of 5 items Eg,[5,8,12,6,8]
21st Apr 2018, 10:55 AM
kukogho gabriel
kukogho gabriel - avatar