What is Array in java

What is Array in java :-

      In Java Array is a Predefined class present in Java. Lang. reflect package.
  • But in technical word arrey is a data structure that hold similar type of element or homogeneous element.
  • Array is a static data structure that means when we construct an array we are unable to choose the size of the array.
  • In java array element is either primitive data type or array element are of object reference.
What is Array in java

  

Step of developing array program in java :-

       When we develop an array program in java then we are bound to follow 3 step.

 Step -1 

    Array Declaration :-

            In java we are bound to declare an array.  When we declare an array we never specify the size of the array.
    Example :-
                        int arr[ ] ;                        stack                                             stack
                        String str[ ] ;                              arr                                            str                
                                                            4 byte of memory                       4 byte of memory
          
  • When we declare the array we indicate compiler about the type of the array.
  • As in the above example arr is an array reference of int type, It must hold the base address of an array where the array element are of int type.
  • After JVM construct an array of int type assign the base address of the array through arr.
  • Where as in the above example str is also an array reference that hold the base address of an array where the element are reference of the same class.
  • After JVM construct an array of string type assign the base address of the array to str.
                   

 Step -2

       Array Construction :-
                  
                   In java array is constructed at the runtime.
  • when we construct an array we are bound to specify the size of the array. By specifying the size of the array programmer indicate the JVM how much byte of memory JVM should allocate one array.
  • When the array is constructed JVM auto initialize array element to the default value of the array type and then assign the base address of the array to the array reference.
  • After the array is constructed if the programmer want to extract the  size of the array then the programmer use the length variable of array class.
      Example :-  arr =new Int[3];
                        str =new String[3]; 
        
    if we take Length variable of Array class.
     int size 1 =arr. Length;   //3
    int size 2 =str. Length;  //3
                 

Step - 3

    Array Initialization :-

        It means the programmer putting the element  into an array by the help of array index.
  • When we retrieve the element of the array and array index from the range the program must terminate at the runtime that give ArrayIndexOutOfBoundsException.
Note :-
         System recognized appropriate address of the array by the help of a mathematical calculation  
 i.e.  base address +(Index *size of data type)
 Example :-
    arr[0] = 12; if the base address is 1200 then 1200 +(0*4) =1200.
    arr[1] = 23; if the base address is 1200 then 1200 +(1*4) =1204.
    arr[2] = 45; if the base address is 1200 then 1200 +(2*4) =1208.

   str[0] =new String("Red"); 2000+(0*4) = 2000.
   str[1] =new String("Green"); 2000+(1*4) = 2004.
   str[2] =new String("Blue"); 2000+(2*4) = 2008.


Program for Array :- 
public class array
{
public static void main(String args[])
{
//step1
int arr[];
String str[];
          //step2
               arr =new int[3];
               str =new String[3];
               System.out.println("see the elements of 1st array is constructed");
               for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
                System.out.println("see the elements of 2nd array after array is constructed");
for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
//step3
   arr[0]=12;
   arr[1]=23;
   arr[2]=45;
        str[0]=new String("Red");
str[1]=new String("Green");
str[2]=new String("Blue");
  System.out.println("see the elements of 1st array after array is initialized");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("see the elements of second array after array initialized");
  for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
}
}
Output :-
What is Array in java


Program - 2
public class test
{
 public static void main(String args[])
  {
int aa[];  //aa is an array reference of int type.
aa= new int[1];
System.out.println(aa[0]);
aa[0]=15;
System.out.println(aa[0]);
call(aa); //the call() method having an argument which is reference of an array int type.
System.out.println(aa[0]);
  }
    static void call(int jj[]) //here the parameter is jj which is array reference of int type.
  {
jj[0]++;
  }
}
output :-
What is Array in java
for more detail follow my Instagram id - https://www.instagram.com/asitmaharana10/

Thank you.