Syntax:
datatype arrayName[] = { list of values };
Within the {} we can specify any number of values separated by a comma.
- When we are declaring, creating and assigning the values in a single line, then we need not specify the size of the array.
- The creation of array will be done by JVM and the size of the array is also decided by JVM based on number of values specified within the {}.
- Once the size of the array is decided the JVM itself will assign the values to the array element
Example: int [] arr = {10,20,30,40,50}
Program
class ArrayDemo {
public static void main (String[] args) {
int[] iarr = {12,34,45,67,89};
for(int x iarr) {
System.out.println(x);
}
double []darr = {1.1,2.2, 3.3,4.4,5.5,6.6};
for(double y: darr) {
System.out.println(y);
}
char carr[] = ('a','b','c','d');
for(char z:carr) {
System.out.println(z)
}
}
}