Sunday, 7 July 2013

ARRAYS in JAVA


                                                                  ARRAYS  IN JAVA


Some interesting facts about arrays in java :

A)  It is never legal to include the size of array at the time of array    declaration in JAVA.
int[7] marks;    is invalid
Because jvm does not allocate space until you actually initiate array object, then size matters.


B) Array objects created on heap. 

C) int array[][] = new int[3][];  is acceptable in java 
    but
    int[][] array = new int[][3];   is not acceptable 

   because jvm needs to know the size of object assigned to the     variable array.

D) Anonymous Array Creation :

  int[] test;
  test = new int {4,7,5};

In this anonymous array we construct and initialize and array and then assign the array to previously declare array reference variable.
      The preceding code create a int array object with three elements 4,7,5 and assign this object to previously declared array reference test.
       We do not specify the size  of anonymous array obj. , size is derived from the number of elements in the curly braces .
So  :   new Object[3] {null, new object(), new object()};
is invalid  size must not be specified.

E)  Just in Time array argument :

       public class Foo {
        void takeArray(int[] someArray)
        { 
             //  use array parameter
        }

       public static void main (String[] args) {
              Foo f = new Foo();
              f.takeArray(new int[]  {1,2,3,4,5});
        }
    }

      Here we are not assigning array object to any reference variable, just-in-time array is used as an argument to method.







No comments:

Post a Comment

Search This Blog

Total Pageviews