Sunday, July 13, 2008

SCJP Exam - Objective 1.3 Part II

Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names.

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.

There are three things, what you should know about array:
  1. declaring array
  2. constructing array
  3. initializing array

Declaring arrays
Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring array of primitives
Declaration of an array of primitives looks like this:
<primitive type>[] <identifier>;

for example:
int[] integer_array;

There is another way to declare arrays (like in language C):
<primitive type> <identifier>[];

for example:
int anotherArray[];
Declaring array of objects
Declaration of array of objects:
<classname>[] <identifier>;

for example:
String[] stringArray;
Array of object can be declared as in language C too.

Declaring multidimensional arrays
Java let you to declare multidimensional arrays. Multidimensional arrays are arrays of arrays.
A two dimensional array declaration:
String[][] twoDimensionalArray;

The
String[][] threeDimensionalArray[];

declaration is interesting, but legal.

Constructing arrays
Constructing an array means creating the array object on the heap. To create an array object, Java must know how much space to allocate on the heap, so user must specify the size of the array at creation time. The size of the array is the number of elements the array will hold.

Examples
int[] intArray; //Declare an one-dimensional array
intArray = new int[3]; //Construct an int array to hold 3 int value
long longArray = new long[1]; //Declare and construct a long array with one element


Constructing multidimensional array
Remember, multidimensional arrays are simply arrays of arrays. It means, elements of multidimensional arrays are arrays.
The
int[][] array = new int[2][];

means we declare and construct a two-dimensional integer array and the size of the first dimension will be 2. When we initialize the array then we can put int array as elements with different size into the array.
The
int otherArray = new int[3][2];

means we declare and construct a two-dimensional integer array and the size of the first dimension will be 3 and declare and construct three one-dimension array with length 2 and put them into the array.

Initializing arrays
Initializing an array means putting things (primitives, object references) into it:
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
int b[] = new int[2];
b[0] = 8;
b[1] = 9;
int[][] c;
c = new int[2][];
c[0] = a;
c[1] =b;
String[] s = new String[3];
s[0] = "hello";
s[1] = null; //null can be use as string element
s[2] = new String("World");

Notice, array indexes begin with 0 and go to size-1. If we try refer to an index that not exist (for example a negative number, or a number greater then size-1) then ArrayIndexOutOfBoundsException trowed.

Declaring, constructing and initializing arrays
It's possible to declare, construct and initialize array in the same time:
int[] a = { 5, 6, 7 };
String lenovo = new String("Lenovo");
String[] computerProducers = { "Dell", lenovo, "HP", "Apple" };

Anonymous array creation
Anonymous array creation can be used to construct and initialize an array, and then assign the array to a previously declared array reference variable:
String[] operatingSystems;
operatingSystems = new String[] { "Windows XP", "Windows Vista", "OpenSuSE 10.3" };

Multidimensional array creation
Multidimensional array creation looks like this:
int[][] array = { { 1, 2, 3 }, { 10, 11 }, { 0 } };

Declare, initialize and uses arrays, enums, objects as static, instance and local variables
Example code:

You can download source code here.

No comments: