Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact

Home Page

AKGBackup - The backup program

            

 

Section 1 :Declaration and Access Control

 


 

Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization.

 


 

The declaration of an array provides some information to the compiler such as the type of the data the array will hold and its name. To indicate that a variable of type array is being declared, a pair of brackets is used. For example:

               

            float [] temperature ;

 

This statement does not creates any array, it only creates a name, which can be used to store reference to an array. The arrays are implemented as objects in Java and hence the name of the array does not stores the array, instead it stores a reference to array.

 

If you wish to declare a multi-dimensional array, you use more than one pair of brackets.

 

            float [][] temperature;

 

The size of the array is indicated while creating the array using the new operator.

 

            temperature = new float [6];

 

Here an array is created which would hold six items of type float. This array will be referred to by the name temperature. You can also use any expression that evaluates to an integral type except long at runtime, to denote the size of the array. So, instead of 6, you can write 2*3, or even an expression containing variables whose values will be known only at runtime. The value returned by the dimension expression must be of integral type, or a compile time error occurs. Each expression, undergoes unary numeric promotion. The promoted type must be int, or a compile time error occurs. This means specifically that the type of a dimension expression must not be long .

 

These two statements are generally combined in a single statement:

 

            float [] temperature = new float [6];

 

Once an array is created, its length never changes. To make an array variable refer to an array of different length, a reference to different array (which must be of the same type) must be assigned to the variable. After this array is created, its data can be accessed by index values. The elements of an array are accessed by using non negative integer index value. The first element of an array is referred to as temperature [0], since indexing in Java mostly begins with 0.

 

Unless all the elements of an array are explicitly initialized, they are initialized automatically with the default values of the element type of the array. In the above example, the default values of all the six items of data will be 0.0, since it is the default value for float.

 

To initialize an array, each of the individual elements is initialized separately.

 

           temperature [0] = 34.6;

           temperature [1] = 35.6; and so on.

Or you can use a loop to initialize the elements of an array.

 

If the values of the elements of an array are known at compile time, an alternative way of declaring and initializing an array can be used.

 

            float [] temperature = {34.6,35.6,36.6,37.5,32.9,30.8};

 

There is no restriction on the order of pair of brackets and the variable name. So, the following code is perfectly valid:

 

            float temperature [] = {34.6,35.6,36.6,37.5,32.9,30.8};

 

The component type of an array may be one of the basic types such as char, byte, short, int, long, float, double or boolean, or it may be a reference type. It may be an array itself.

 

While creating a multidimensional array, dimensions must be created left to right.

So, int [] [] array = new int [] [4]; is not valid.


 

 


 

section1-1 | section1-2 | section1-3 | section1-4

 

Sections : 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

 

 

 

 

 

 

Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact  

Loading

 Number of Pages viewed on this site since January' 2003 : Hit Counter eXTReMe Tracker

For any queries, comments or suggestions, write to me .

This site never compromises your privacy, please read this site's privacy policy.