Java Arrays Introduction - Explained in detail

Опубликовано: 17 Июнь 2026
на канале: Learn Java
231
8

You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.

Arrays in java :-
==================
datatypes---Arrays--Collections

A java array can be used to overcome the drawbacks that we have with the ordinary data types. It is actually used to store the collection of similar kind of data by using a single variable name and array internally uses contiguous memory allocation.

syntax for Declaration:-
~~~~~~~~~~~~~~~~~~~~~~~~~
datatype[] variableName; valid
datatype []variableName; valid
datatype variableName[]; valid
[]datatype variableName; invalid

values=100,200,300,400,500

int[] numbers; This one is highly recommended
int []numbers;
int numbers[];
[]int numbers; invalid one

Declaration and instantiation:-
~~~~~~~~~~~~~~~~~~~~
int[] numbers;

syntax for instantiation:-
~~~~~~~~~~~~~~~
int[] numbers; declaration
numbers=new int[5]; //5 set of values, the size must be declared and it is always fixed

int[] numbers=new int[5];//This is the typical java array with the declaration and instantiation

How to initialize the values to a java array?
We will perform the initialization just by using the variable name and the index number.
int[] a=new int[5];//0 1 2 3 4
if the size of the array is n, the indices will be (0 to n-1)=0 to 4
a[0]=100;
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;

a[5]=600;//invalid -Array Index out of bound

2nd way of doing the initialization:-
~~~~~~~~~~~~~~~~~~~~~~~
int[] a= {100,200,300,400,500};

The above example is used to assign the values directly without declaring the size and internally the memory will be allocated in the similar way.