Wed. May 1st, 2024

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) 

        } 

   } 

} 

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *