Declaring, Creating Assigning the Array elements in Single line

476 views.

Syntax

datatype arrayName[]={list of values};

Within the {} we can specify any number of values separated by 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 size of the array is also decided by JVM based on number 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};

int[] arr={10,20,30,40,50};   <—————————————>int[] arr;

arr=new int[5];

arr[0]=10;

arr[1]=20;

arr[2]=30;

arr[3]=40;

arr[4]=50;

Program:

 

Leave a Reply