Array is a collection of similar type of data. It is fixed in size means that you can't increase the size of array at run time. It is a collection of homogeneous data elements. It stores the value on the basis of the index value.
Advantage of Array
One variable can store multiple value: The main advantage of the array is we can represent multiple value under the same name.
Code Optimization: No, need to declare a lot of variable of same type data, We can retrieve and sort data easily.
Random access: We can retrieve any data from array with the help of the index value.
Disadvantage of Array
The main limitation of the array is Size Limit when once we declare array there is no chance to increase and decrease the size of an array according to our requirement, Hence memory point of view array concept is not recommended to use. To overcome this limitation in Java introduce the collection concept.
Types of Array in Java
There are two types of array in Java.
- Single Dimensional Array
- Multidimensional Array
Array Declaration
Single dimension array declaration.
Syntax : int[] a;
int a[];
int []a;
Note:
1) At the time of array creation we must be specify the size of array otherwise get an compile time error. For Example
int[] a=new int[]; Invalid.
int[] a=new int[5]; Valid
2) If we specify the array size as negative int value, then we will get run-time error, NegativeArraySizeException.
3) To specify the array size the allowed data types are byte, short, int, char If we use other data type then we will get an Compile time error.
4) The maximum allowed size of array in Java is 2147483647 (It is the maximum value of int data type)
Program:
public class ArrayEx { public static void main(String []args) { int arr[] = {10,20,30}; for (int i=0; i < arr.length; i++) { System.out.println(arr[i]); } } }Output:
10 20 39- Progamming is what actually means ?
- What is software development ?
- How we categorized software development in different manner.
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle