Course Content

Course Content

Description

Array and Declaration of Array in Java

In the last video of our Core Java course we learnt what are Identifiers in Java and which identifiers are used while working with Java programming language. Now, in this video we are going to learn about what is array in Java and how to declare array in Java with types of arrays in Java.

An array in Java programming is a collection of variables of the same type. Always first element of the array is stored at 0 index. There are two main advantage of Java array, ie. Code Optimization and Random Access. Code optimization makes the code optimized and we can retrieve or sort the data easily. And with the help of Random access we can get any data located at any index position.

The disadvantage of Java Array is Size Limit. We can store only fixed size of elements in the array. It doesn't grow it's size at runtime.

Array in Java are further divided into two different types, Single Dimensional Array and Multi Dimensional Array.

Syntax to declare single dimensional array in Java -
datatype[] arr_name; (or)
datatype arr_name[];,
Example : int[]roll_no; (or) int roll_no[];

Syntax to declare Multi dimensional array in Java -
datatype[][] var_name; (or)
datatype var_name[][]; (or)
datatype[] var_name[];
Example : int[][]roll_no; (or) int roll_no[][]; (or) int[]roll_no[];

Syntax of single dimensional array -
Var_name=new data_type[Size];
Example : int roll_no=new int[5];

Syntax of Multi dimensional array -
Var_name=new data_type[rows][columns];
Example : int roll_no=new int[2][3];

Now we will create a Program for Single dimensional array in Java.
class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];
a[0]=10;
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
Output :
10
20
70
40
50

Now we will create a Program for Two dimensional array in Java.
class twod
{
public static void main(String args[])
{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for (int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
}
Output :
1 2 3
2 4 5
4 4 5

Thus, this is everything about what is Array in Java programming and how to declare array in Java. We have also covered different types of Arrays in Java with their examples and syntaxes. Now, in the next video we will see what are the Operators of Java and how many of them are there. 

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere