What is Java Arrays and how it works ?

By |2018-03-22T05:56:45+05:30April 25th, 2015|java|

In Java arrays are represented by a class called java.util.Arrays. It is an utility class, to define several utility methods to work with array. The class Arrays is considered to be a static factory as this is having all static methods.

What is Java Arrays ?

Array is a container, which holds a fixed number of values in a single type. It is a mandatory  to  give a size of an array at the time of initialization and the size which we are giving is fixed. Here are some important points about Java arrays.

  • Java Arrays are index based collection.
  • The values in the array are stored in the memory from the left to right.
  • The default values of the array. “0” in the case of primitives, “null” in the case of Objects, “false” in the case of boolean.
  • Arrays are fixed length in size.
  • Array is homogeneous data structure, so that an array can store a uniformed elements.
  • The main advantage of an array is we can represent multiple values under the same name. So that readability of code is improved.
  • To specify the array size, the allowed data types are byte, short, int and char, If we use any other data type the compiler will generate the exception.

Though arrays having multiple above advantages as seen above, in the mean while its having some disadvantages too.

  • The Arrays are in fixed length,
  • The Array is homogeneous data structure, so that an array can store a uniformed elements.

To overcome these above two disadvantages using Java collection framework.

How to Create Java Array ?

Creating an array in Java is two step process.

  • Declaring of an array variable and
  • Initialize an array object.


int array[];     // Declaring
array = new int[10];     // Initializing

Types of an Java Array

  1. Single dimensional arrays
  2. Multidimensional arrays
  3. Anonymous arrays

Single dimensional arrays

Every array in Java represented as an Object, so that we can create an array by using new operator.


int[] a = new int[10];

We can declare and assign values to an array in a single line like below :


int[] a = {5,10,60,50,70};
char[] c = {'a','b','c','d'};

At the time of initialization of an array, we should mention the size of it.  Other wise we will get compilation error.


int[] a = new int[];  // Compilation Error
int[] a = new int[-1];  // Runtime Exception (NegativeArraySizeException)
int[] a = new int[10];   // Legal

Java Array Single Dimentional Example :


public class ArrayDemo {

    public static void main(String[] args) throws InterruptedException {
        int[] array;
        array = new int[10];
        array[0] = 10;
        array[1] = 8;
        array[2] = 5;
        array[3] = 15;
        array[4] = 9;
        array[5] = 7;
        for (int i = 0; i < array.length; i++) {
            System.out.println(" a[ " + i + " ]" + array[i]);
        }
    }
}

Output :

a[ 0 ] 10
a[ 1 ] 8
a[ 2 ] 5
a[ 3 ] 15
a[ 4 ] 9
a[ 5 ] 7
a[ 6 ] 0
a[ 7 ] 0
a[ 8 ] 0
a[ 9 ] 0

The above program represented in the memory like below.

Arrays in Java

Java Multidimensional arrays

In Java multidimensional arrays are represented as arrays of arrays. The main advantage of this approach is memory utilization will be improved.


<span style="color: #003300;">int[][] a = new int[3][];</span>

<span style="color: #003300;">a[0]=new int[2];</span>

<span style="color: #003300;">a[1]= new[1];</span>

<span style="color: #003300;">a[2] = new[3]</span>

The above example represented in memory

 

Multidimentional-Array

Array Single Line Declaration,Initialization


int[][] a={{1,5,6,7,},{10,20,30}};

If we want to use this type of declaration we should perform declaration, initialization in one line other wise Compiler will generate exception.

int[] x;
x={10,20,30};  // Illegal State Exception

Java Anonymous Arrays

In the case of anonymous arrays we can specify the size. Other wise we will get compilation error.


new int[]{5,10,15,20,25};

The main objective of anonymous arrays are just for instant usage.

public class AnonymousArrayDemo {
    public static void main(String[] args) {
        sum(new int[]{1,2,3});
    }
    public static void sum(int[] x){
        int total=0;
        for (int i : x) {
            total +=i;
        }
        System.out.println("Total : "+total);
    }
}

Output :

Total : 6

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment