1. Array

  • Primitives are stored in the stack memory only.
  • Array objects are in heap.
  • Heap objects are not continuous.
  • Since array objects are in heap they MAY not be continuous (we know that in languages like C++ and C arrays are continuous).
  • Whether array is continuous or not is totally up to JVM to decide.
  • scroll down for the codeblock.

String Array :

String[] arr = new String[5];

  • here the reference variable arr is stored in the stack and it points to the string object which lives in Heap Memory and has 5 elements.
  • each of these elements are in itself an object which are stored in different parts of the heap memory. And reference variables are assigned for each object to store there addresses.
  • Now by default all reference variables point to null so arr[0] (which is a reference variable pointing to the object where a string is stored) will point to null.
  • So the "O"s in the image below are actually an array of reference variables pointing to an object.
  • Pasted image 20240101234556.png
  • Pasted image 20240225173645.png
package com.hitarth;


import java.util.Arrays;

public class temp {
    public static void main(String[] args) {
        // Syntax of ARRAY //
        //datatype[] variable_name = new datatype[size];

        // store 5 roll numbers:
        int[] ros = new int[5]; // RHS is object and LHS is the reference variable. // we can write as int arr[] also (like we do in C++)
        // "new" is used to create an object.

        // or directly
        int[] ros2 = {23, 12, 45, 32, 15}; // the reference variable on the LHS will point to the object on the RHS // even tho we are not writing the "new" keyword here we are still creating a new object here, internally it does the same thing as using the "new" keyword.

        int[] ros3; // declaration of array, ros is getting defined in the Stack
        ros3 = new int[5]; // initialisation: here object is being created in the Heap Memory

        // if we try to print an empty / uninitialised array we'll get zeros. So by default an array is initialised with 0s.
        System.out.println(ros[2]); // → 0
        System.out.println(ros3[2]); // → 0 (if we had not done "ros3 = new int[5];" this then we'd get an error as we would not have initialised an array then).
        // also printing an index greater than the index of array will give an error


        /* String Array */
        String[] arr = new String[4]; // note that String is an object
        System.out.println(arr[2]); // → null

    }
}

For Each Loop + Printing the array + Arrays.toString(), Array of primitives and Objects :

package com.hitarth;


import java.util.Arrays;

public class temp {
    public static void main(String[] args) {
        /*ARRAY OF PRIMITIVES*/
        int[] arr = {1,2,3,4,5};

    /* Printing an array */

        // Method 1 :
        for (int i=0; i<arr.length; i++)
        {
            System.out.print(arr[i] + " ");
        }// → 1 2 3 4 5

        // Method 2 :
        //Enhanced For Loop : FOR EACH Loop
        for(int num : arr) { // for every element in the array print the element // we can iterate through a string array like this as well.
            System.out.print(num); // here num represents element of the array
        } // → 12345

        // Method 3 :
        System.out.println(Arrays.toString(arr)); // → [1, 2, 3, 4, 5]
            // It basically converts it into a string


        /*ARRAY OF OBJECTS*/

        String[] str1 = {"bruh", "lol", "dawg"};
        String[] str2 = new String[4];
        str2 = new String[]{"hello", "lmao", "dawg", "real"};
        str2[1] = "not lmao";

        System.out.println(Arrays.toString(str2)); // → [hello, not lmao, dawg, real]
    }
}

2D array :

package com.hitarth;


import java.util.Arrays;

public class temp {
    public static void main(String[] args) {
        /* 2D Array - Matrix */

        int[][] arr = {
                {1,2,3},
                {4,2,1},
                {3,7,4}
        };
        // this is basically an array of arrays

        for (int i=0; i< arr.length; i++)
        {
            for(int j=0; j<arr[i].length; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
        
        /*
        1 2 3
        4 2 1
        3 7 4
        */
    }
}

Array List : (Dynamic Array)

package com.hitarth;


import java.util.ArrayList;
import java.util.Arrays;

public class temp {
    public static void main(String[] args) {
        /* Array List */
        ArrayList<Integer> list = new ArrayList<Integer>(10); // 10 is the initial capacity here. Also, we ignore the "Integer" in "new ArrayList<Integer>" and write it as "new ArrayList<>" as well.
        // "list" is a reference variable.

        list.add(67); // to add elements in to the array list
        list.add(100);
        list.add(12);
        list.add(14);

        System.out.println(list); // → [67, 100]
        // to print the list

        //typing "list." will show all the available functions that we can apply on the list. For e.g. :
        System.out.println(list.contains(67)); // → true
        list.set(0, 99); // sets the 0th index to the value 99
        list.remove(2); // removes the value at index 2

        System.out.println(list.get(1)); // → 100 // gets the element at index 1
        // list[index] syntax doesn't work here

    }
}