February 5, 2011

Arrays in Java

Arrays in Java are represented as objects. When an array is constructed all its elements are initialized with defaults values (irrespective of whether the array is declared as an instance variable or local variable).

Constructing an array of object references constructs only the array objects and not the objects themselves.

public class Test {

   private static int[] intArr = new int[3];

   public static void main(String[] args) {

      Test[] testArr = new Test[2];

      for (int i=0; i < testArr.length; i++) {
         System.out.println(testArr[i]);
      }

      for (int j=0; j < intArr.length; j++) {
         System.out.println(intArr[j]);
      }

      intArr[0] = 3;
      testArr[1] = new Test();
   }
}

1 comment:

  1. When I click on "finalize method in Java" in the code examples section in brings me to Arrays in Java instead. The link is incorrect.

    ReplyDelete