What is Array ?
Array is used to store multiple values of same datatype inside a single variable.
Array is index based , it's index starts from 0.
Array only store Homogeneous data (All the elements should have same datatype.
Array has fixed size , once it created size can't be changed.
Why array ?
Without Array :
- If we want to store name of 5 students , without array we create 5 variables and store the names in that 5 variables.
Example:
public class Without_Array {
public static void main(String[] args) {
String name1="Raksha";
String name2="Rakshan";
String name3="Varun";
String name4="Roshan";
String name5="Abi";
System.out.println("Name 1:"+name1);
System.out.println("Name 2:"+name2);
System.out.println("Name 3:"+name3);
System.out.println("Name 4:"+name4);
System.out.println("Name 5:"+name5);
}
}
Output:
Name 1:Raksha
Name 2:Rakshan
Name 3:Varun
Name 4:Roshan
Name 5:Abi
Here , we need 5 variables to store 5 names.
With Array:
- Here , If we want to store 5 names of students . We can store them inside a single variable.
Example:
public class Array {
public static void main(String[] args) {
String[] names= {"Raksha" , "Rashmi" , "Sangeetha" , "Kavitha" , "Saranya"};
System.out.println("Name 1 :"+names[0]);
System.out.println("Name 2 :"+names[1]);
System.out.println("Name 3 :"+names[2]);
System.out.println("Name 4 :"+names[3]);
System.out.println("Name 5 :"+names[4]);
}
}
Output:
Name 1 :Raksha
Name 2 :Rashmi
Name 3 :Sangeetha
Name 4 :Kavitha
Name 5 :Saranya
Here , we can store all the 5 names in a single variable.
Length of an Array:
- The length property is used to find the number of elements in an array.
- It returns the total size of the array.
Syntax:
arrayName.length;
Example:
public class Array_Length {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Array Length: " + numbers.length);
}
}
Output:
Array Length: 5
Important: length is a property, not a method. So we should not use parentheses.
Array Declaration and Initialization:
Declaration + Initialization :
- When we can declare the array , and initialize it in the same time it is called declaration + initialization .
Syntax:
dataType[] arrayName ={value1, value2 , value3};
Example:
String[] names= {"Raksha" , "Rashmi" , "Sangeetha" , "Kavitha" , "Saranya"};
Declaration:
- Declaring the array variable with its size.
Syntax:
dataType[] arrayName = new datatype[array_size];
This is declaration.
Example:
int[] numbers = new int[5];
Default Values of Arrays :
- When an array is created using the new keyword, Java automatically assigns default values to its elements.
Data type Default value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
char '\u0000'
boolean false
String null
Object null
Example:
public class Array_Default {
public static void main(String[] args) {
int[] arr=new int[2];
String[] arr2=new String[3];
float[] arr3=new float[4];
char[] arr4=new char[3];
System.out.println("Default value of Integer Array :"+arr[0]+" ," +arr[1]);
System.out.println();
System.out.println("Default value of String Array :"+arr2[0]+" ," +arr2[1] +" ," +arr2[2]);
System.out.println();
System.out.println("Default value of Float Array :"+arr3[0]+" ," +arr3[1] +arr3[2]+" ," +arr3[3]);
System.out.println();
System.out.println("Default value of Characterr Array :"+arr4[0]+" ," +arr4[1] + " ,"+arr4[2]);
}
}
Output:
Default value of Integer Array :0 ,0
Default value of String Array :null ,null ,null
Default value of Float Array :0.0 ,0.00.0 ,0.0
Default value of Characterr Array :
Initialization:
- We can declare the array variable and initialize it later.
Syntax:
datatype arrayName;
arrayName = {value1, value2 , value3 , value4 , value5};
Example:
int[] a;
a = new int[]{10, 20, 30};
Advantages of Array :
- Reduce the number of variables.
- Easy to access using index.
- Stores multiple values in a single variable.
Disadvantages of Array:
- Fixed in size
- It can only stores the values of same datatype .
- Accessing an invalid index causes ArrayIndexOutOfBoundsException.

Top comments (0)