Saturday 11 June 2016

The Java Language:Arrays:Array Creation and Initialization

The new operator is used to create an instance of an array. After the new operator, we specify the base type of the array and its length with a bracketed integer expression:

 arrayOfInts = new int [42];  
 someStrings = new String [ number + 2 ];  
We can, of course, combine the steps of declaring and allocating the array:
 double [] someNumbers = new double [20];  
 Component [] widgets = new Component [12];  
Array indices start with zero. Thus, the first element of someNumbers[] is 0, and the last element is 19. After creation, the array elements are initialized to the default values for their type. For numeric types, this means the elements are initially zero:
 int [] grades = new int [30];  
 grades[0] = 99;  
 grades[1] = 72;  
 // grades[2] == 0  
The elements of an array of objects are references to the objects—just like individual variables they point to—but do not actually contain instances of the objects. The default value of each element is therefore null until we assign instances of appropriate objects:
 String names [] = new String [4];  
 names [0] = new String();  
 names [1] = "Boofa";  
 names [2] = someObject.toString();  
 // names[3] == null  
This is an important distinction that can cause confusion. In many other languages, the act of creating an array is the same as allocating storage for its elements. In Java, a newly allocated array of objects actually contains only reference variables, each with the value null.6 That’s not to say that there is no memory associated with an empty array; memory is needed to hold those references (the empty “slots” in the array). Figure 4-4 illustrates the names array of the previous example.
names is a variable of type String[] (i.e., a string array). This particular String[] object contains four String type variables. We have assigned String objects to the first three array elements. The fourth has the default value null. Java supports the C-style curly braces {} construct for creating an array and initializing its elements:
 int [] primes = { 2, 3, 5, 7, 7+4 }; // e.g., primes[2] = 5  
An array object of the proper type and length is implicitly created, and the values of the comma-separated list of expressions are assigned to its elements. Note that we did not use the new keyword or the array type here. The type of the array was inferred from the assignment. We can use the {} syntax with an array of objects. In this case, each expression must evaluate to an object that can be assigned to a variable of the base type of the array or the value null. Here are some examples:
 String [] verbs = { "run", "jump", someWord.toString() };  
 Button [] controls = { stopButton, new Button("Forwards"),  
 new Button("Backwards") };  
 // All types are subtypes of Object  
 Object [] objects = { stopButton, "A word", null };  
The following are equivalent:
 Button [] threeButtons = new Button [3];  
 Button [] threeButtons = { null, null, null };  

0 comments:

Post a Comment