CodeNewbie Community 🌱

Cover image for Conclusion to arrays
Abhann
Abhann

Posted on

Conclusion to arrays

In the final section of arrays, we will discuss;

o The Length variable and the Length()(i.e. Length method).
o Anonymous Arrays.
o Array Element Assignments.
o Array Variable Assignments.

o Length Vs Length()
The statements below describe their uses, and examples to back-up follows;

 •    Length variable is a final variable applicable for arrays, 
    and length variable represents the size of the array.
Enter fullscreen mode Exit fullscreen mode

image

 •    We cannot apply the “length()” to the size of an array, 
    else you will get a compile time error.
Enter fullscreen mode Exit fullscreen mode

image

 •  The length() is a final method applicable for only string 
    objects and it returns the number of characters present in 
    the string given.
Enter fullscreen mode Exit fullscreen mode

image

Note: Length Variable is applicable for arrays, but not for string objects, whereas the length() is applicable for string objects, but not for arrays, as clearly stated above.

More Examples;
imageThe first invalid is because, the length() is not applicable for any array size and this clearly includes the string array size, whereas the latter is because the length variable is not applicable for string objects and this clearly includes string object elements within an array as well, this is a good approach to summarize all we have discussed leading to the last example.

•   In multi-dimensional arrays, length variable represents 
    only base size, but not total size.
Enter fullscreen mode Exit fullscreen mode

Observe below;
image

• There is no direct way to find the total length of a 
    multi-dimensional array, but indirectly, we can determine 
    it as follows;
Enter fullscreen mode Exit fullscreen mode

image

o Anonymous Arrays
Sometimes we can declare an array without a variable name, such type of nameless arrays is called anonymous arrays.
The main purpose of anonymous arrays is for instant use (i.e. one-time usage).
We have defined an anonymous array and also stated its purpose, the next thing would be to create one, and then see how it works.
Observe below;
image

There are some rules guiding anonymous arrays, as follows;

• While creating anonymous arrays, we can’t specify the 
    size, otherwise, we will get a compile time error.
Enter fullscreen mode Exit fullscreen mode

Ex;
image
Error;
image

• It is completely valid and possible to create a multi- 
    dimensional anonymous array.
Enter fullscreen mode Exit fullscreen mode

Ex;
image

• Based on our requirements, we can give a variable name to 
    an anonymous array, but doing this will make the array no 
    longer anonymous.
Enter fullscreen mode Exit fullscreen mode

Ex;
image

o Array Element Assignment
Case 1; Primitive Type Arrays
In the case of primitive type arrays as array elements, we can assign any type in regard to the data type that is compatible with it, which can be implicitly promoted to the declared type.
Ex;
image
Below is the progressive data type assignment diagram.
imageFurther-more; in the case of float-type arrays, the allowed data types are; byte, short, char, int, long and float.

Case 2; Object Type Arrays
In the case of object type arrays, as array elements, we can provide either a declared type object or it’s child class object.
Ex;
image
Another Example;
image
Case 3; Interface Type Arrays
For interface type arrays, as array elements, the array implementation class objects are allowed.
Ex;
image

Summary;image

o Array Variable Assignments
Case 1:
Element level promotions are not applicable at array level.
Ex;image
As seen above, char data types can be promoted to int types, whereas a char array cannot be promoted to an int array.
Further explanation;image
Case 2:
Whenever we are assigning one array to another array, internal elements won’t be copied, just reference variables will be re-assigned.
Ex;image
Case 3:
When we assign an array to another array, the dimensions must be matched.
In the case of a one-dimensional array, we should assign a one-dimensional array only, if we try to assign any other dimension, then we will get a compile time error.
Ex;image
Note; Whenever we are assigning one array to another, both dimensions and types must be matched, but sizes are not required to match (i.e. internal elements).

In conclusion, let’s try the exercises below:
o Which of the following promotions would be performed automatically?
image

o Consider:
image
Q.(i) How many objects are created overall?
(ii) How many objects are eligible for garbage collection?

Top comments (0)