Declaration, Instantiation and Initialization of Java Array

We can declare, instantiate and initialize the java array together by:

  1. int a[]={33,3,4,5};//declaration, instantiation and initialization  

Let's see the simple example to print this array.

  1. //Java Program to illustrate the use of declaration, instantiation   
  2. //and initialization of Java array in a single line  
  3. class Testarray1{  
  4. public static void main(String args[]){  
  5. int a[]={33,3,4,5};//declaration, instantiation and initialization  
  6. //printing array  
  7. for(int i=0;i<a.length;i++)//length is the property of array  
  8. System.out.println(a[i]);  
  9. }}  

    Output:

    33
    3
    4
    5
Posted on by