by Michael Thomas
by Michael Thomas
Objectives (topics)
Topic | Notes |
Arrays | Arrays
|
Array, declaration | Declaration Examples
Compile Errors:
|
Array declaration & initialization | Declaration of a Array - to declare that an array will
contain a certain data type. The array does not exist in memory yet. Initialization of an Array - to create an array object in memory. If data is passed via the {} identifier then the array is populated with the elements passed, else the array is populated with a default value. Default value for Data Types that extend Object is the null value. Defaults for primative integers is 0, floating point is 0.00, boolean is false, and char is \u0000 . Declaration only: int k []; Declaration must always come before initialization. Remember this, you can not use the {} to load an array once the array is initialized. That's why you can only use the {} in the initialization of the array. Also, int k[]= new int[ 5 ] {0,1,2,3,4}; is a compile error because the array was initialized as of: int k[]= new int[ 5 ] Use the {} in the initialization of an array when you know what elements will
reside in the array. Use the [] when you don't know how many elements the array will have at the time of declaration. Ex: int k []; Later you can initialize the array with k = new int [intArraySize] Use the [5] when you know how many elements the array will have at the time you declare and initialize the array in one statement. Ex int k [] = new int [5] |