Java Array Notes
Java Array Notes
Advantages:
Represent huge number of values using single variable so that readability of the code will be
improved
Limitations:
Arrays are fixed in size. Once we create an array, we cant increase or decrease size based on our
requirement. Hence to use arrays we should be aware of the size in advance, which may not be
possible always
Array Declaration
1D array declaration
Int[] x;
Int []x;
Int x[];
All 3 are valid declarations. But we are recommended to use the first one as it seperates the variable
from the data type
At the time of declaration, we cant specify the size otherwise we’ll get compile time error
o Int[6] x is invalid
o Int[] x is valid
2d array declaration
All the declarations other than the last one are valid.
If we are specifying the variable after the dimension[], its applicable only for the first variable. If we are
trying to apply for remaining variables we will get compile time error
3D array declaration
Array creation
Every array in java is a object. Hence we can create arrays using new operator
We can create objects only for classes. For every array type, corresponding classes are available and
these classes are part of java language and not available to the programmer level
At the time of creation, we should specify the size else we will get compiler error
Int [] x = new int[0] is valid. Array of size 0 is valid.
If we are trying to specify array size with negative int value then we will get runtime exception
saying NegativeArraySize Exception eg: int[] x = new int[-6]
To specify array size, the allowed data types are int, char, byte and short
The max size of int array is 2147483647 which is the max value of int value type
2D Array creation
In Java, two dimensional arrays is not implemented by using matrix style. SUN people followed array of
arrays approach for multidimensional array creation. The main advantage of this approach is memory
utilization will be improved.
Eg1:
Eg2: 3D array
Array Initialization:
Note:
Whenever we are trying to print any reference variable, internally toString() method will be called which
is implemented by default to return the string in the following format: classname @ hashcode in
hexadecimal form eg: [I @3e2345
Eg:
Every array will be initialized with default values, if we are not satisfied with default values then we can
override these values with our values eg:x[0] =10, x[2] =30
We can declare create and initialize an array in a single line ( shortcut representation)
Int[] x = {10,20,30}
Char[] c ={‘a’,’e’,’i’}
For multidimensional array: we can use this shortcut for multidimensional array also
If we want to use this shortcut, we should perform all activities in a single line. If we are trying to split it
into multiple lines then we will get compile time error.
Length vs Length()
Length
Length is a final variable applicable for arrays. Length variable represents the size of array.
s.o.p(x.length) -> 6
Length():
Sop(s.length()) -> 5
Note: Length variable is applicable for arrays and not applicable for string objects and length() method
is applicable for string objects but not for arrays
In Multi-dimensional array length variable represents only the base size not the total size
Sopln(x[0].length) -> 3
There is no direct way to find total length of multi dimensional array but indirectly we can find as
follows:
X[0].length + x[1].length+…
Anonymous Arrays
We can declare arrays without name, such type of arrays are called as anonymous array.
While creating anonymous array if we specify the size, we’ll get CE if we provide size Eg: new int[3]
{10,20,30}
In the case of object type arrays as array elements we can provide either declared type objects or its
child class objects
Case3:
We can create interface type arrays and it can hold objects of its implementing class
Tabular form:
Array variable Assignments
Case1:
Ex: char element can be promoted int type but char array cannot be promoted to int array
For Object arrays, child class arrays can be used to point to parent class array
Ex:
Case2:
Whenever we are assigning one array to another array, internal elements will not get copied, only the
reference variables will be reassigned.
Case3:
Whenever we are assigning one array to another array, the dimensions must be matched.
Ex:
In the place of 1D int array we should provide 1D array only. If we are trying to provide any other
dimension then we’ll get compile time error.
Note: when we copy one array to another array types and dimensions should match but sizes need not
to be matched