Array important points in java

In this section we will discuss important point that we should know about arrays in java.


Important point :

  At the time of Declaration of an array we can not specify the size of the array in java.
int [5] // wrong ...

  Every array in Java is an object , Hence we can create it by using new operator.
int a[ ]=new int[5]

  For every array type corresponding classes are available. These classes are not applicable for Programmer Level.

  At the time of array Construction/Instantiation compulsory we should specify the size of an array otherwise we will get Compile-Time error.
int a[ ]=new int[ ];//error

  we can have array of size= 0 in java
int a[ ]=new int[0 ]; //correct

  If we specify size of an array in negative . Then we will get run time Exception saying NegativeArraySizeException .
int a[ ]=new int[-3 ]; //error-NegativeArraySizeException

  To specify the size of array in Java , the allowed data types are byte,short,int and char. if we use any other data type then we will get error.
int a[]=new int['A']//correct

Program for getting Name of Array Class.

//example of getting the array class
class Example {

    public static void main(String[] args) {
        int a[] = {2, 3, 23, 5};
        String name = a.getClass().getName();
        System.out.println("Name of array a  = " + name);

        double d[] = {1.3, 4.3};
        String nameD = d.getClass().getName();
        System.out.println("Name of array d =" + nameD);

        String sr[] = new String[34];
        String nameS = sr.getClass().getName();
        System.out.println("Name of array sr = " + nameS);

    }
}
Name of array a  = [I
Name of array d =[D
Name of array sr = [Ljava.lang.String;