Array is a data structure in the java. That helps us to Store data in an efficient way.
1) Using the new keyword with size only:
Syntax:
int[] array_name = new int[array_size];
Use this method when you know the number of elements you want but don’t have their values yet.
Example:
int[] profit = new int[5];
We create an array of size 5 where each element is a default value i.e (0 of integers).
Let's print this array:
int[] profit = new int[5];
System.out.println(profit);
Output:
[I@659e0bfd
We got this.
To get the elements from this array is pretty simple by using the indexing:
int[] profit = new int[5];
System.out.println(profit[2]);
Leave a comment
You must be logged in to post a comment.