wellnwill Marathahalli Bangalore

Java Array


  • Array represents a finite sequence of similar type of data that have contiguous memory locations.
  • Array class is a public final so inheritance is not possible.
  • Array is also known as homogeneous data structure.
  • The elements of the array is always accessed by using an integer index. The index begins from (0).
  • In java, arrays are created dynamically (at runtime) as per the dimension and data type. It means arrays are the objects in java.
  • In java, each array object contains a length variable of final type that represents the number of element available in an array.

Types of Array

  • Single dimensional array
  • Multidimensional array

There are two activity involved when we use simple variable i.e.

  • Variable declaration
  • Variable initialization

Some of the activities are involved when we use array in java i.e. Array declaration, construction, initialization

Array declaration

single dimensional array Syntax- [modifier] []; Ex: int[] a; , float []a2; , short a3[]; , String s1[];

A , a2, a3, s1 are the reference variable which can contain null or address of an array object.

Array construction

in this stage array elements will be initialized with default values. Syntax- = new [size]; Ex- a=new int[4];

Array initialization with user defined value

case 1:- int a[]={1,2,3,4};
              String s1[]={“Raj”,”Ram”,”Rama”};
case 2:- int[] a=new int[10];
              a[0]=1;
              a[1]=2;
              a[2]=3;
              a[4]=4;

Array declaration: Multidimensional array

Syntax- [modifier] [][]; Ex-: int[][] a1; , short a3[][]; , String []s1[];


Array construction

Syntax-  = new [size][size];
Ex- a1=new int[3][2];

Array initialization with user defined value

case 1:- int a[][]={1,2,3,4}; case 2:- int[][] a=new int[3][2]; // 3 arrays with size 2 a[0][0]=1; a[0][1]=2; a[1][0]=3; a[1][1]=4; a[2][0]=5; a[2][1]=6;

class Test213
{
  public static void main(String args[])
  {
    int a[];
    a=new int[2];
    System.out.println(a[0]);
    System.out.println(a[1]);
  }
}
// Output - 0,0
  class Test214
{
  public static void main(String args[])
  {
    int a[]=new int[2];
    a[0]=1;
    a[1]=2;
    for(int i=0;i

class Test215
{
   public static void main(String args[])
   {
     int a[]=new int[2];
     a[0]=1;
     a[1]=2;
     for(int temp:a)
     {
       System.out.println(temp);
     }
   } 
} 

//  Output - 1,2
class Test216
{
  public static void main(String args[])
  {
    String s[]=new String[2];
    s[0]="Raj";
    s[1]="Ram";
    for(String temp:s)
    {
      System.out.println(temp);
    }
  }
} 

// Output - Raj, Ram

class Test217
{
  public static void main(String args[])
  {
     String s[]=new String[2];
     s[0]="Raj";
     s[1]="Ram";
     Object obj=s;
     String s1[]=(String[])obj;
     for(String temp:s1)
     {
        System.out.println(temp);
     }
  }
}

// Output - Raj,Ram

Enhanced For Statement


The enhanced for-loop is mainly used to access the elements from Collection/Array sequentially. It is also known as for-each loop. Syntax-

for( variable_name : array/collection )
{
                     //statements
}

We cannot access data in reverse order by using for-each loop. We cannot get data directly for the particular index.

Instanceof operator

Instanceof operator verifies that whether the given reference variable is a instance of specified type (class or subclass or interface) or not.In other word, instanceof operator is used to check the type of the object.It returns Boolean value as a return value.

class Parent
{

}

class Child1 extends Parent
{

}

class Child2 extends Parent
{

}


class Test218
{
  public static void main(String args[])
  {
     Parent p1=new Child1();
     Child1 c1=new Child1();
     System.out.println(p1 instanceof Child1);
     System.out.println(p1 instanceof Child2);
     System.out.println(c1 instanceof Parent);
     System.out.println(p1 instanceof Parent);
  }
}

// Output - true,false,true,true


class Parent
{

}

class Child1 extends Parent
{

}

class Test0218
{
  public static void main(String args[])
  {
     Parent p1=new Parent();
     System.out.println(p1 instanceof Parent);
     System.out.println(p1 instanceof Child1);
  }
}

// Output - true,false
Footer with Map